未显示输入验证的错误消息

时间:2010-11-12 07:20:10

标签: php forms validation zend-framework

我刚接触Zend Framework请帮帮我......

// create text input for pharmacy name
  $name = new Zend_Form_Element_Text('name');
  $name->setOptions(array('size' => '30'))
    ->setRequired(true)
    ->addValidator('NotEmpty')
    ->addValidator('Alpha');
  $name->getValidator('NotEmpty')->setMessage('Please enter pharmacy name.');
        $name->getValidator('Alpha')->setMessage('PharmacyName can only contain letters and spaces.');


  // create submit button
  $submit = new Zend_Form_Element_Submit('submit');
  $submit->removeDecorator('HtmlTag');
  $submit->setLabel('Save')
  ->setOptions(array('class' => 'submit'));

  // attach elements to form
  $this->addElement($cd)
   ->addElement($name)
   ->addElement($submit);

1 个答案:

答案 0 :(得分:1)

我使用构造如下定义元素参数:

$this->addElement('text', 'o_title', array(
        'label'      => 'title....',
        'required'   => true,
        'filters'    => array('StringTrim','WordLength'),
        'validators' => array(
            array('validator' => 'StringLength', 'options' => array(0, 40,'utf8',   
                                  'messages'=>array('stringLengthTooLong'=>'Text too long'))),
                   array('NotEmpty', true,  
                             array('messages'=>array('isEmpty'=>"Field must not be empty")))
            ),
        'maxlength' => 40,
        'decorators' => array('Uniform'),
        'description'=> 'input only 40 chars'
    ));

验证器在上面的代码中以两种方式指定.... 每个验证器都可以有很多消息......