【问题标题】:can't figure out how to validate zend_form无法弄清楚如何验证 zend_form
【发布时间】:2010-07-07 23:42:36
【问题描述】:

我正试图弄清楚如何让这个 zend 表单进行验证。我不明白:

addValidator() 参数是特定的验证器吗?是否有这些验证器的列表?

我在 forms/contact.php 中有这个:

类 Application_Form_Contact 扩展 Zend_Form {

public function init()
{
    $this->setAction('index/process');
    $this->setMethod('post');

    $name = new Zend_Form_Element_Text('name');
    $name->setLabel('Name:');
//  $name->addValidator('alnum');
    $name->setRequired(true);

    $email = new Zend_Form_Element_Text('email');
    $email->setLabel('Email:')->setRequired(true);

    $confirm = new Zend_Form_Element_Text('confirm');
    $confirm->setLabel('Confirm Email:')->setRequired(true);

    $phone = new Zend_Form_Element_Text('phone');
    $phone->setLabel('Phone:')->setRequired(true);

    $subject = new Zend_Form_Element_Select('subject');
    $subject->setLabel('Subject:')->setRequired(true);
    $subject->setMultiOptions(array('Performance'=>'Performance', 
                                    'Workshop'=>'Workshop',
                                    'Other'=>'Other'
                                ));


    $message = new Zend_Form_Element_Textarea('message');
    $message->setLabel('Message:')->setRequired(true);
    $message->setAttrib('rows','6');
    $message->setAttrib('cols','30');

    $submit = new Zend_Form_Element_Submit('Submit');



    $this->addElements(array(   $name, 
                    $email,
                    $confirm, 
                    $phone, 
                    $subject, 
                    $message,
                    $submit 
));


           $this->setElementDecorators(array
        ('ViewHelper',

        array(array('data' => 'HtmlTag'), array('tag' => 'td')), 
        array('Label' , array('tag' => 'td')), 
        array(array('row' => 'HtmlTag') , array('tag' => 'tr'))

        ));

    $submit->setDecorators(array('ViewHelper',

         array(array('data' => 'HtmlTag'), array('tag' => 'td')),
         array(array('emptyrow' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'PREPEND')),
         array(array('row' => 'HtmlTag') , array('tag' => 'tr'))
        ));                 

    $this->setDecorators(array(
        'FormElements',

        array('HtmlTag' , array('tag' => 'table' , 'class' => 'formTable')),
        'Form'
    )
    );
}
}

我的控制器是:

public function indexAction()
{

    $this->view->form = new Application_Form_Contact();

}

public function processAction()
{
//              $this->view->form = new Application_Form_Contact();
//    
     if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();

            //  echo 'success';
                $this->view->data = $formdata;

            }   else {
        //        $form->populate($formData);
            }


}

我是新手,所以我可能会犯一些我看不到的明显错误。我正在尝试进行基本验证:

  • 必须填写所有字段
  • 所有 html 都被剥离
  • 发送电子邮件并确认
  • 电子邮件字段必须匹配
  • 电子邮件必须是有效格式。

任何帮助将不胜感激!

【问题讨论】:

    标签: zend-framework zend-form


    【解决方案1】:

    你试过 isValid() 吗?:

    $form = new forms_ContactForm();
    
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                echo 'success';
                exit;
            } else {
                $form->populate($formData);
            }
        }
    
        $this->view->form = $form;
    

    from

    关于验证器:

    $firstName = new Zend_Form_Element_Text('firstName');
        $firstName->setLabel('First name')
                  ->setRequired(true)
                  ->addValidator('NotEmpty');
    
        $lastName = new Zend_Form_Element_Text('lastName');
        $lastName->setLabel('Last name')
                 ->setRequired(true)
                 ->addValidator('NotEmpty');
    
        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email address')
              ->addFilter('StringToLower')
              ->setRequired(true)
              ->addValidator('NotEmpty', true)
              ->addValidator('EmailAddress'); 
    

    这里是关于 Zend fiorm 和验证器的 Zend 文档的链接。 Creating Form Elements Using Zend_Form_Element

    【讨论】:

    • 好吧,如果我有适当的验证器,那么它可能会知道它是否有效,但我不知道如何使用 addValidators()
    • @Iznogood 很好,谢谢!你能指出我的验证者列表吗?我一直在寻找高低。
    • @Joel 再次编辑。从那里你总是可以在谷歌上搜索 zend 表单验证器并从那里继续。那里有很多信息。
    • 酷!而且,我刚刚在图书馆本身找到了它们。感谢您的帮助!
    • @Joel 没问题,感谢您接受我的回答!
    猜你喜欢
    • 2014-12-18
    • 1970-01-01
    • 2014-08-29
    • 2012-01-27
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多