1

I'm trying to change the validation message for zfcuser register form. I have try to change the message by using following code in bootstrap.php:

$filter->get('email')->setErrorMessage('Email address has been used', \ZfcUser\Validator\AbstractRecord::ERROR_RECORD_FOUND);

However, this line of code changes all my email invalid message but what I want to achieve is different validation messages for different errors.

For example:

Email exist => 'Email address has been used'
Invalid format => 'Email not valid'
Not input => 'Please input your email'

Anyone can share with me anyway to achieve that?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ewe Tek Min
  • 855
  • 10
  • 19
  • 2
    You need to [set the error message on the validator](http://zf2.readthedocs.org/en/latest/modules/zend.validator.messages.html) rather than the form input. It would also make sense to do so when creating it (rather than after) – AlexP Apr 21 '14 at 02:11
  • do you mean i can subclass the validator and set the error message myself? – Ewe Tek Min Apr 21 '14 at 15:05
  • You can hint to the [input filter when you create the form](https://zf2.readthedocs.org/en/latest/modules/zend.form.quick-start.html#hinting-to-the-input-filter), they all allow an `'options' = array()` argument. – AlexP Apr 21 '14 at 18:30

1 Answers1

0

Try this. Maybe it's ugly, but it seems that it should work

$filter = $form->getInputFilter();
$validators = $filter->get('email')->getValidatorChain()->getValidators();
foreach ($validators as $validator) {
    if ($validator['instance'] instanceof \ZfcUser\Validator\AbstractRecord) {
        $validator['instance']->setOptions(array(
            'messageTemplates' => array(
                'recordFound' => 'Email address has been used',
            ),
        ));
    }
}
Grigory
  • 11
  • 3