如何解决Zend表单getValue需要有人解释吗?
| 我只是在Zend Framework中使用Zend_Form,遇到了一些很奇怪的事情。 我的loginAction中包含以下内容$form = new Application_Model_FormLogin();
if ($this->getRequest()->isPost()) {
$email = $form->getValue(\'email\');
$pswd = $form->getValue(\'pswd\');
echo \"<p>Your e-mail is {$email},and password is {$pswd}</p>\";
}
提交时仅输出
Your e-mail is
和password is
所以我检查了一下print_r发生了什么,
print_r($ form-> getValues());
print_r($ _ POST);
其中显示以下内容,
数组([email] => [pswd] =>)数组([email] => asd [pswd] => asd [submit] =>登录)
因此,表单值数组将两个值都设置为null,并且全局发布数组具有正确的值。现在我无法解决问题了?
现在,我确实设法解决了这个问题,但是我需要帮助来了解为什么会起作用?我所做的就是将loginAction更改为此。
$form = new Application_Model_FormLogin();
if ($this->getRequest()->isPost()) {
//Added this in
if ($form->isValid($this->_request->getPost())) {
$email = $form->getValue(\'email\');
$pswd = $form->getValue(\'pswd\');
echo \"<p>Your e-mail is {$email},and password is {$pswd}</p>\";
}
}
我不知道这是如何工作的?考虑到没有验证字段?
有什么想法吗?我能想到的是,也许我的服务器配置中有些设置很奇怪?
谢谢
解决方法
您没有在表单对象中加载值。
通常,您检查表单是否有效,并为此加载后数据,在下一步中,您可以使用getValue()从表单中获取(过滤后的)值。
if($this->getRequest()->isPost()) {
$form = new My_Form();
if($form->isValid($this->getRequest()->getPost())){
echo $form->getValue(\'fieldname\');
}
}
, isValid()是实际填充表单对象中字段的内容,直到您这样做时,表单对象中还不存在这些值。
修改原始代码就是这样简单
if ($this->getRequest()->isPost()) {
//your $form object has none of your POSTed values
$form->isValid($this->getRequest()->getPost())
//now your form object has the POSTed values and you can access them
$email = $form->getValue(\'email\');
$pswd = $form->getValue(\'pswd\');
echo \"<p>Your e-mail is {$email},and password is {$pswd}</p>\";
}
这非常轻松地略过了http://framework.zend.com/manual/1.11/en/zend.form.quickstart.html#zend.form.quickstart.validate
还考虑此示例,可能更有意义。在这里,您只需从POST获取值。
if ($this->getRequest()->isPost()) {
$email = $this->getRequest()->getPost(\'email\');
$password = $this->getRequest()->getPost(\'password\');
echo \"<p> Your email is $email and your password is $password </p>\";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。