Model view controller Zend表单不验证输入
Zend Form今天不是我的朋友 这项工作:- 控制器:-Model view controller Zend表单不验证输入,model-view-controller,zend-framework,zend-form,Model View Controller,Zend Framework,Zend Form,Zend Form今天不是我的朋友 这项工作:- 控制器:- public function indexAction() { $loginForm = new Application_Form_Login(); //is there a submitted form? if($this->getRequest()->isPost()){ //yes there is so process it. $f
public function indexAction()
{
$loginForm = new Application_Form_Login();
//is there a submitted form?
if($this->getRequest()->isPost()){
//yes there is so process it.
$formdata = $this->getRequest()->getPost();
if($loginForm->isValid($formdata)){
$user_logon = $loginForm->getValue('user_name');
$user_pw = $loginForm->getValue('user_pw');
if($this->authenticate($user_logon, $user_pw)){
$this->_redirect();
}
} else {
$this->view->errors = $loginForm->getMessages();
}
}
$this->view->loginForm = $loginForm;
形式
这可不行
控制器
public function addAction()
{
$addform = new Application_Form_Student_Add();
//has a form been submitted?
if($this->getRequest()->isPost()){
if(isset($_POST['Cancel'])) $this->_redirect('/student');
$formdata = $this->getRequest()->getPost();
if($addform->isValid($formdata)){
Zend_Debug::dump($formdata);
} else {
$this->view->errors = $addform->getMessages();
}
}
$this->view->addForm = $addform->generate();
}
形式
两者都正确显示,但是第一个将验证,第二个不会验证。当提交空白表单时,第一个表单将显示错误消息,但是第二个表单似乎总是通过验证,而不管输入的值如何
我已经看了好几个小时了,可能需要其他人来看看代码,并指出对我来说显而易见的东西
在这两种情况下,视图都只是回显表单。在工作代码中,您使用$this在init中引用表单;在第二个版本中,您使用$this->studentForm 所以我很想知道为什么这里的代码不同,第二个init的对象是什么。你发布的代码与这里不同 在你的第二个控制器中,我知道你应该使用
$addform->studentForm->stuff
因为它不是Zend_表单的实例,而是一个包含studentForm Zend_表单的对象。谢谢,你说得对。我没有注意到代码中的差异。正如我所说,我已经看了太久了!再次感谢。嘿,欢迎你!请记住接受答案,当他们帮助和投票@vascowhite欢迎使用stackoverflow,请在此处阅读常见问题:
public function init()
{
$this->studentform = new Zend_Form();
$baseUrl = new Zend_View_Helper_BaseUrl();
$action = $baseUrl->baseUrl() . "/student/add";
$this->studentform->setAction($action);
$this->studentform->setName('addStudent');
$this->studentform->setMethod('post');
$student_title = new App_Form_Element_Text('student_title');
$student_title ->setLabel('Titletest')
->setRequired(true);
$cancel = new Zend_Form_Element_Submit('Cancel');
$submit = new Zend_Form_Element_Submit('Submit');
$this->studentform->addElement($student_title);
$this->studentform->addElement($cancel);
$this->studentform->addElement($submit);
}
$addform->studentForm->stuff