How to customize Zend_Form regular expression error messages?

I have the following code:

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

It creates an input field in the form and sets the rule for checking the regular expression and works fine.

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

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

(where the input was POSTCODE)

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

+4
source share
3 answers

I think you can set the error message in Validator:

$postcode = $form->createElement('text', 'postcode'); $postcode->setLabel('Post code:'); $postcode->addValidator('regex', false, array( 'pattern' => '/^[az]{1,3}[0-9]{1,3} ?[0-9]{1,3}[az]{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 this does not work, try

  • setErrorMessages ($ message arrays) : Add some error messages to display form validation errors by overwriting any previously set error messages.
+5
source

If you define your validator as an 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 the example above, the validator for the form is no different from other types of Zend_Validate_ * instances.

Setting up verification messages includes searching for API Documents and determining the message constant for this verification error (as in the case of Zend_Validate_Alnum :: INVALID). Of course, if your IDE provides good context autofill, just entering a validation class might be enough - since in most cases the message constants are really clear.

Another way is to use the Zend_Form magic methods and just pass the β€œmessage” as a parameter to your validator:

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

This will internally call the setMessages () method defined in Zend_Validate_Abstract, and essentially it's just a reduction or time delay defined for Zend_Form's.

NB: there is a highlighted section in the ZF manual for verification messages.

+1
source

You can use the original Zend zip 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' ) ); 
0
source

Source: https://habr.com/ru/post/1305670/


All Articles