Dynamic form validation

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Dynamic form validation

Hi all,

An issue I come across reasonably regularly with ZF2 forms is dynamically setting validation rules based on other elements in the form.

For example, let’s say I have a checkout form, where I capture users personal details and payment details.  So would could maybe have a PaymentForm, with a PaymentDetails fieldset and a PersonalDetails fieldset.  Each fieldset could have its own input filter and hence validate itself when the form is validated.  All good.

Now, what if we want the user to either be able to pay via credit card or PayPal.  For this we might take the PaymentDetails fieldset further, by adding two sub fieldsets, CreditCardDetails and PayPal details.  Within the PaymentDetails fieldset there might be a select option which allows the user to choose a credit card type or PayPal.  If they choose PayPal, any values in the CreditCard fieldset are irrelevant, and shouldn’t be validated.

I belive I should be using validation groups for this, but at what point do I set the validation group, should I do this in my controller action where the form is submitted, but before it is validated:  Essentially getting the submitted value for the ‘payment type’ select option and manipulating the form input groups accordingly??  This I believe will work, but it would seem to tie the forms underlying logic to the controller a bit too much.

Am I missing something, or would this be the ‘recommended way’?


List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]