How can I customise Zend_Form regex error messages

2019-05-05 01:04发布

I have the following code:

    $postcode = $form->createElement('text', 'postcode');
    $postcode->setLabel('Post code:');
    $postcode->addValidator('regex', false, 
        array('/^[a-z]{1,3}[0-9]{1,3} ?[0-9]{1,3}[a-z]{1,3}$/i'));
    $postcode->addFilters(array('StringToUpper'));
    $postcode->setRequired(true);

It creates an input field in a form and sets a regex validation rule and works just fine.

The problem is that the error message it displays when a user enters an invalid postcode is this:

'POSTCODE' does not match against pattern
    '/^[a-z]{1,3}[0-9]{1,3} ?[0-9]{1,3}[a-z]{1,3}$/i'

(where input was POSTCODE)

How can I change this message to be a little more friendly?

3条回答
我只想做你的唯一
2楼-- · 2019-05-05 01:28

You could use the original Zend postcode validator

$user->addElement('text', 'postcode', array('label' => 'Postcode *',
    'required' => true,        
    'class' => 'postcode_anywhere',
    "validators" => array(
        array("NotEmpty", false, array("messages" => array("isEmpty" => "Required *"),)),
        array('PostCode', false, array('locale' => 'en_GB')
        )
    ),
    'filters' => array(array('StringToUpper')),
    'class' => 'text'
        )
);
查看更多
时光不老,我们不散
3楼-- · 2019-05-05 01:33

I think to remember, you can set the error message in the Validator:

$postcode = $form->createElement('text', 'postcode');
$postcode->setLabel('Post code:');
$postcode->addValidator('regex', false, array(
    'pattern' => '/^[a-z]{1,3}[0-9]{1,3} ?[0-9]{1,3}[a-z]{1,3}$/i')
    'messages' => array(
        'regexInvalid'   => "Invalid type given, value should be string, integer or float",
        'regexNotMatch' => "'%value%' does not match against pattern '%pattern%'",
        'regexErrorous'  => "There was an internal error while using the pattern '%pattern%'"
    )
);
$postcode->addFilters(array('StringToUpper'));
$postcode->setRequired(true);

If that doesn't work, try

  • setErrorMessages(array $messages): add multiple error messages to display on form validation errors, overwriting all previously set error messages.
查看更多
劳资没心,怎么记你
4楼-- · 2019-05-05 01:36

If you define your validator as external variable use setMessage():

$validator = new Zend_Validate_Alnum();
$validator->setMessage('My custom error message for given validation rule',
                       Zend_Validate_Alnum::INVALID);
$formElement->addValidator($validator);

As you see in example above validator for form doesn't differ from any other kind of Zend_Validate_* instances.

Setting up validation messages involves looking into API Docs and finding out message constant for a given validation error (as I did in case of Zend_Validate_Alnum::INVALID). Of course if your IDE provides good context auto-completion just typing the validator class can be enough - as message constants are really self-explanatory in most cases.

Another way would be to use Zend_Form's magic methods, and simply passing 'messages' key, as a parameter to your validator:

$formElement->addValidator(array(
  'alnum', false, array('messages' => array(
     Zend_Validate_Alnum::INVALID => 'my message'
   ))
));

This would internally trigger the setMessages() method defined in Zend_Validate_Abstract, and in essence just a short-cut/time-saver defined for Zend_Form's.

NB: There's a dedicated section in ZF Manual regarding validation messages.

查看更多
登录 后发表回答