Zend_Form -> Nicely change setRequired() validate message

前端 未结 10 1307
死守一世寂寞
死守一世寂寞 2021-01-31 09:25

Say I create a text element like this:

$firstName = new Zend_Form_Element_Text(\'firstName\');
$firstName->setRequired(true);

Whats

相关标签:
10条回答
  • 2021-01-31 10:20

    Zend_Form sets the required validation error as 'isEmpty', so you can override its message using setErrorMessages(). For example:

    //Your Required Element
    $element->setRequired(true)->setErrorMessages(array(
    'isEmpty'=>'Please fill this field'
    ));
    

    It worked for me, using ZF 1.11

    0 讨论(0)
  • 2021-01-31 10:21

    if you put:

    $element->setRequired(false);
    

    the validations don't work at all, you have to define:

    $element->setAllowEmpty(false);
    

    in order to get the correct behavior of the validations.

    0 讨论(0)
  • 2021-01-31 10:23

    use a zend translator with zend_validate.php from

    ZendFramework-1.11.3\resources\languages\en\Zend_Validate.php and then modify this file how you need

    and then modify it accordingly to your needs

    0 讨论(0)
  • 2021-01-31 10:25

    One small issue. This code:

    $zipCode->setLabel('Postal Code')
            ->addValidator('StringLength', true, array( 5, 5 ) )
            ->addErrorMessage('More than 5')
            ->addValidator('Digits', true)
            ->addErrorMessage('Not a digit');
    

    Will generate both error messages if either validation fails. Isn't is supposed to stop after the first fails?

    0 讨论(0)
提交回复
热议问题