Custom validation function not activated

In my form’s model I have a custom validation function for a field defined in this way




...

public function rules()

{

    return [

        ['birth_date', 'checkDateFormat'],


        // other rules

    ];

}


public function checkDateFormat($attribute, $params)

{

    // no real check at the moment to be sure that the error is triggered

    $this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));

}



The error message doesn’t appear on my field.

I’m working on the Signup native form, so to be sure that it is not a filed problem, I’ve set the rule

[‘username’, ‘checkDateFormat’]

and removed all the other rules related to the username field, but the message doesn’t appear neither for it.

I’ve tried passing nothing as parameters to checkDateFormat, I’ve tried to explicitly pass the field’s name to addError()

$this->addError(‘username’, ‘…’);

but nothing appears.

Which is the correct way to set a custom validation function?

Your custom validator won’t have client side validation automatically so you will need to submit the form and call validate(). Are you doing that?