Welcome Guest, Not a member yet? Register   Sign In
Form Submit
#1

[eluser]kidego32[/eluser]
Hi all,

After working a bit with CI (about a week), I have a question regarding the MVC structure. When you have a view submit a form, is the general practice to have the same controller that called the view receive the form, or do you have a new controller handle the form submit?

For example:

Customer_model (represents the customer mysql table)
customer (controller)
customer/add (method that calls the add_view)
add_view (the view that displays the form to enter the customer details)

Should the add_view submit to the customer/add (the calling controller/method) or should it submit to another method (ex: customer/save)?
#2

[eluser]Scriptor[/eluser]
I think the User Guide validation example has it so that the form submits it to the original controller method, but that might have been done just to keep things short. The validation library should work either way (as long as you put the validation-specific code in the right method, of course).

Using the same method has the advantage that the user's URL doesn't change if the form's data isn't accepted. Since you're using the same method you just have to tell it to load the view, but this time show the errors.

On the other hand, having one giant method that handles both presenting the form, and processing it, would be very bulky and prone to bugs. A separate method would leave you free to separate the code, an added benefit is that no unnecessary validation/processing would be run when the user first loads the form.

A solution would be to use another method (customer/save), then if validation fails saving the error data somewhere (flashdata or sessions) and redirecting back to customer/add, which would show the form with the errors.




Theme © iAndrew 2016 - Forum software by © MyBB