1

如果Zend_Translator应用于. 绝对不可能设置自定义元素验证消息Zend_Form。请看我的例子...

class App_Form extends Zend_Form 
{

    public function init() 
    {

        parent::init();

        //let's create and set translator for our form
        $translations = array(
            Zend_Validate_NotEmpty::IS_EMPTY => 'Value is required for this field.' //just one line of translations as example
        );
        $adapter = new Zend_Translator('array', $translations);
        $this->setTranslator($adapter);

        //now we're going to add some text element
        $element = new Zend_Form_Element_Text('firstname');
        $element
            ->setRequired(true)
            ->setFilters(array('StringTrim'))
            ->addValidator('NotEmpty')
        ;

        //now we're overriding error message for key with name Zend_Validate_NotEmpty::IS_EMPTY
        $element->getValidator('NotEmpty')->setMessage('Please enter your first name.', Zend_Validate_NotEmpty::IS_EMPTY);
        $this->addElement($element);

        if (!$this->isValid(array('firstname' => ''))) {
            print_r($this->getErrorMessages());
            //we'll still get "Value is required for this field." message
            //if we will try to disable translator we'll get correct message "Value is required for this field."
        }

    }

}

调用方法时会发生此错误Zend_Form::getErrorMessages()。此方法使用已翻译的消息模板覆盖当前消息模板。

所以,不修改似乎不可能完成这个任务Zend_Validate_Abstract

有任何想法吗?

4

0 回答 0