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

The second: "the form will submit the data whatever then the server sides returns the error message". In order to do this you must call $form->isValid($this->_request->getPost()) in your action.

If it is not valid then you need to send data back to the user:

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

For client side validation you can use http://docs.jquery.com/Plugins/Validation

于 2012-09-04T10:04:01.490 に答える