Welcome Guest, Not a member yet? Register   Sign In
Handling multiple forms on a single view - what crafty ways are there to handle this?
#11

[eluser]nikefido[/eluser]
That still involves two sets of rules / fields. If you just set the rules for every form element within the same controller, it's going to test for all of those rules regardless of which form was submitted.

So it seems to me that you either need to add some flow structure in your controller to test which form to set the rules/fields for, or set the forms to submit to different controllers.

Either way, you end up repeating code, especially for data that you need to pass to your view on every call.
#12

[eluser]Michael Ekoka[/eluser]
Well, this is a design flaw on CodeIgniter's part. The best approach for 2 forms remains to have 2 actions inside your controller to handle your submissions. You can create a third (private) method inside the controller that abstract validation for both forms if you don't feel like repeating code.

Normally, validation should be handled from a third party perspective, since it involves data that can be inserted/manipulated from multiple points of the application.

In some framework, I've seen it be done inside the Model. Although I still do not agree with that approach, it's better than doing it in your controller. You can still, set your rules inside your model and simply call that method from your model, but for custom validation rules, you have to set them inside your controller.
#13

[eluser]gon[/eluser]
Quote:So it seems to me that you either need to add some flow structure in your controller to test which form to set the rules/fields for, or set the forms to submit to different controllers.

Either way, you end up repeating code, especially for data that you need to pass to your view on every call.

You could use a single action for both forms, and send the hidden field to know which form it is.

In the action, you could have an array containing all validation rules from both forms.
And another array that stores which rules must be set for every form.
Then you would build the validation rules array looking at these 2 arrays.

And you shouldn't have to put conditions.


Having done this in the past, now I would go for 2 actions.
Repeating code? Why you couldn't factor the common code in private functions on the controller, or as libraries?
I say private functions cause these utility functions shouldn't be actions for the controller.


cheers
#14

[eluser]Crimp[/eluser]
I always set validation rules and fields in private functions. It allows for re-use and keeps the controllers cleaner in terms of displaying the logic. If there are, for example, additional fields in an 'edit' version of a form (a user signs up with minimal info, edits more fields), I include an argument in the validation setting function and a switch for the added fields.
#15

[eluser]mikedfunk[/eluser]
[quote author="Crimp" date="1208617274"]I always set validation rules and fields in private functions. It allows for re-use and keeps the controllers cleaner in terms of displaying the logic. If there are, for example, additional fields in an 'edit' version of a form (a user signs up with minimal info, edits more fields), I include an argument in the validation setting function and a switch for the added fields.[/quote]

Thanks for this. It's really helpful to reuse validation code like this. Note - because of variable scope, it only works if you run the set_rules and set_fields functions inside the private function.




Theme © iAndrew 2016 - Forum software by © MyBB