1

我认为 $form->addVlidator 函数可以作为一些前端表单检查的东西,但是当我添加它时,即使输入无效,似乎表单仍在提交中。

或者我错了,它更像是控制器端检查,这意味着表单将提交数据,然后服务器端返回错误消息?

我的代码是这样工作的。

表格类:

<?php
class Application_Form_Test extends Zend_Form

{
    public function init()
    {

        $this->setName('stdForm');
        //$this->setMethod('post');
        //$this->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'my-lovely-form'));
        $this->setAttrib('enctype', 'multipart/form-data');

        $this->setAction('somewhere')
             ->setMethod('post');


        $username = $this->createElement('text', 'name', array('label' => 'Username:'));
        $username->addValidator('alnum')
                 ->addValidator('regex', false, array('/^[a-z]+/'))
                 ->addValidator('stringLength', false, array(9, 20, 'messages'=>'Cannot be more than 9 chars'))
                 ->setRequired(true)
                 ->addFilter('StringToLower');

        $email = $this->createElement('text', 'email', array('label' => 'E-mail'));
        $email->addValidator('StringLength', false, array(8))
                 ->setRequired(true);

        $password = $this->createElement('password', 'pass1', array('label' => 'Password'));
        $password->addValidator('StringLength', false, array(6))
                 ->setRequired(true);

         $password2 = $this->createElement('password', 'pass2', array('label' => 'Repeat password'));
         $password2->addValidator('StringLength', false, array(6))
                 ->setRequired(true);

         $message = $this->createElement('textarea', 'message', array('label' => 'Message'));
         $message->addValidator('StringLength', false, array(6))
                 ->setRequired(true)
                 ->setAttrib('COLS', '40')
                 ->setAttrib('ROWS', '4');

            $captcha = new Zend_Form_Element_Captcha('foo', array(
                'label' => "human?",
                'captcha' => 'Figlet',
                'captchaOptions' => array(
                    'captcha' => 'Figlet',
                    'wordLen' => 6,
                    'timeout' => 300,
                ),
            ));

        // Add elements to form:
        $this->addElement($username)
             ->addElement($email)   
             ->addElement($password)
             ->addElement($password2)
             ->addElement($message)
             ->addElement($captcha)
             // use addElement() as a factory to create 'Login' button:
             ->addElement('submit', 'send', array('label' => 'Form sender'));

    }
}

控制器代码:

   public function aboutAction()
{
    $this ->_helper->layout->disableLayout(); 

    $form = new Application_Form_Test();

    $this->view->testForm = $form;        
}

查看文件:

    <?php echo  $this->testForm;?>   
4

1 回答 1

0

第二个:“表单将提交数据,然后服务器端返回错误消息”。为了做到这一点,你必须调用$form->isValid($this->_request->getPost())你的行动。

如果它无效,那么您需要将数据发送回用户:

$form->populate($this->_request->getPost());
$this->view->form = $form;

对于客户端验证,您可以使用http://docs.jquery.com/Plugins/Validation

于 2012-09-04T10:04:01.490 回答