A free and open-source book on ZF3 for beginners


11.2. Using Validation Groups

Sometimes it may be useful to temporarily disable validation of some form elements. You can do that with a feature called validation groups.

By default, all form elements are validated. A validation group allows to disable validation of certain fields.

For example, assume you implement a form named PaymentForm, which allows you to select a payment method of several alternatives (credit card, bank transfer and cash). If the user selects credit card, you also want him to enter the credit card number; else if user selects bank transfer, you want him to enter bank account number; and finally, if the cash is selected, user does not need to enter additional information.

For this form, you will have to dynamically hide and display dependent fields in client's browser with JavaScript.

How would you validate such form in your controller's action? The problem is that some fields depend on others. The card_number field is required only when payment_method is the "credit card", otherwise it is optional. The same is for the bank_account field - it is required only when payment_method is the "bank transfer".

We can handle this case elegantly with the validation group. The Form class provides the setValidationGroup() method, which accepts the list of fields that you want to validate; all other fields will be suppressed and not validated.

// First, we will validate the "payment_method" field.
$form->setValidationGroup(['payment_method']);
if ($form->isValid())
{
    $data = $form->getData();
    
    $paymentMethod = $data['payment_method'];
    
    // Next, validate the dependent fields
    if ($paymentMethod=='credit_card') {
        $form->setValidationGroup(['payment_method', 'card_number']);
    } else if ($paymentMethod=='bank_account') {
        $form->setValidationGroup(['payment_method', 'bank_account']);
    }
    
    if ($form->isValid()) {
        $data = $form->getData();
        
        // Do something with the data
        // ...
    }
}

You can see this example in action in the Form Demo sample web application bundled with this book. Just type "http://localhost/payment" URL in your browser.


Top