表格验证不适用于Zend表格

时间:2011-05-05 18:41:02

标签: php zend-framework zend-form

这是我的zend表单代码

<?php
class Form_Bookmark extends Zend_Form{
    function init(){            
        $url_address = $this->createElement("text", "url_address");
        $url_address->setLabel("Url");        
        $url_address->isRequired(TRUE);
        $bmk_title = $this->createElement("text", "bmk_title");
        $bmk_title->setLabel("Title");
        $bmk_title->isRequired(TRUE);
        $bmk_desc = $this->createElement("textarea", "bmk_desc");
        $bmk_desc->setLabel("Description");
        $bmk_ispublic = $this->createElement("checkbox", "bmk_ispublic");
        $bmk_ispublic->setLabel("IsPublic?");
        $bmk_ispublic->isRequired(TRUE);
        $submit = new Zend_Form_Element_Submit("submit");
        $submit->setValue("Submit");
        $this->addElement($url_address);
        $this->addElement($bmk_title);
        $this->addElement($bmk_desc);
        $this->addElement($bmk_ispublic);
        $this->addElement($submit);
    }
}
?>

这是我的控制器代码

$f = Zend_Controller_Front::getInstance();
        $bookmark_form = new Form_Bookmark();

        $bookmark_form->setAction($f->getBaseUrl()."/user/index");
        $bookmark_form->setMethod("POST");
        if($this->getRequest()->isPost()){           
            if($bookmark_form->isValid($_POST)){

                echo "FORM VALIDATED SUCCESSFULLY";
            }else{
                echo "FORM VALIDATION FAILED";
            }
        }

        $this->view->form = $bookmark_form;

问题是此表单验证不起作用。它总是显示“FORM VALIDATED SUCCESSFULLY”,即使我提交了空表格,但我添加了$ url_address-&gt; isRequired(TRUE);财产“真实”。

1 个答案:

答案 0 :(得分:4)

正确的方法名称是setRequired:

$bmk_title->setRequired(true);