Changing the error message generated by a validator

Changing a single error message without affecting any other elements or forms

The preferred way to do this is to pass an options array to the validator when adding it to an element, with the 'messages' key and this contains another array with the key of the error message and your new error message text. You can use this method to set different error messages for different elements that have the same validator. For example, to change the email address validator message.

1
2
3
$element->addValidator('email', array(
    'messages' => array('invalid' => 'The email address is invalid')
));
$element->addValidator('email', array(
    'messages' => array('invalid' => 'The email address is invalid')
));

The message keys and available placeholders are shown on the Validator options (Advanced) page.

Changing the default error messages (affects other forms and elements)

In some situations (such as translating) it may be faster to just change the default error messages. You will need to go to quform/lib/Quform/Validator/ and find the file for the Validator you want to change. Once you’ve found it, open the file and look inside and near the top of the file you should see a PHP array that sets the default error messages e.g.

1
2
3
protected $_messageTemplates = array(
    'invalid' => 'Invalid email address'
);
protected $_messageTemplates = array(
    'invalid' => 'Invalid email address'
);

This is the messages that is returned to the form when validation fails, change it to whatever you like. Note: some validators have more than one message for different errors.

Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy