We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to cancel validation only for the specific attribute

When you have multiple rules for a single attribute like the code below, when you set 'cancelOnFail' for an attribute for instance for the email, it will stop the whole validation chain if PresenceOf for email fails, What i need is to be able to stop the validation only for email attribute and continue the validation for the rest of attributes, how to achive that?

       $validation->add(
            'email',
            new PresenceOf(
                array(
                    'message' => 'The e-mail is required',
                     'cancelOnFail' => true
                )
            )
        );
        $validation->add(
            'email',
            new Email(
                array(
                    'message' => 'The e-mail is not valid'
                )
            )
        );
      $validation->add(
              'name',
              new PresenceOf(
                  array(
                      'message' => 'The name is required'
                  )
              )
          );

HI!

In such cases, I add rules after a simple test.

if (!empty($request['email']))
{
    $validation->add(
        'email',
        new Email(
            [
                'message' => 'The e-mail is not valid',
            ]
        )
    );
    ....
}
edited Jan '16

You can use two instances of the Validation object. That's what is done in the Form object when the fields and associated validators are declared from there. see https://docs.phalcon.io/en/latest/reference/forms.html#validation