Welcome Guest, Not a member yet? Register   Sign In
Controller implementation for create/edit/preview application flow
#1

[eluser]logan2z[/eluser]
I'm currently implementing a fairly simple classifieds posting system with the usual create->preview->edit->preview->save sort of flow. I'm wondering what the best practice is for implementing the controller logic for this sort of thing. I want all submission of the posting form to go through a single method that performs validation so I don't have to validate in multiple places. What I've got so far looks something like this:

class Listing extends Controller {
public function index() {
$this->load->library('form_validation');
$this->Listing_model->from_array($_POST); // prime model from POST data
if ($this->form_validation->run() == FALSE) {
// first-time form creation, or posted form with errors
$data = ...
$this->load->view('edit_view', $data);
}
else {
// posted form passed validation, display preview
$data = ...
$this->load->view('preview_view', $data);
}
}

public function create() {
$this->Listing_model->from_array($_POST);
$this->Listing_model->insert();
}
}

One issue is that I want the preview page to have an 'Edit' button that will allow the user to make changes to the form data before posting. If the form action for that button ends up calling the controller's index() method, the current implementation will show the 'preview' page rather than the edit page, since the validation will pass. I can split up the controller into separate edit(), preview(), create(), etc. methods but then I think I'll have to have calls to the validator in multiple places, since someone could create a form and post it to my create() method without going through my validation code.

Another thought I had was to either embed a hidden field in the form indicating whether the form is being posted due to an edit request or a create request and then check that field in my central controller method, but I wasn't sure if that was a bit hacky. Alternatively, I could use the 'value' of the 'Edit' and 'Create' buttons passed in the POST data to determine which operation was being requested, but again that seemed a bit hacky.

Anyway, I'm just looking for suggestions for best practices to handle this sort of thing in CI.

Thanks in advance.
#2

[eluser]logan2z[/eluser]
Never mind, I think I've figured it out.
#3

[eluser]Unknown[/eluser]
hi there,

I am having exactly the same issue any chance you could let me know how you managed it?

thanks in advance

Alec




Theme © iAndrew 2016 - Forum software by © MyBB