" />

Zend 形式的 setValue ,视图具有空值,如

发布于 2024-10-06 13:29:52 字数 657 浏览 3 评论 0原文

我有一个使用 Zend-Form 的相当复杂的表单设置。在某一时刻,我使用以下方法设置隐藏输入的值:

$oHidden = new Zend_Form_Element_Hidden('ratings'.$k);        
$oHidden->setValue('ratings');Zend_Debug::dump($oHidden);
$this->addElements(array($oHidden));

此方法在相同形式的其他地方运行良好,但是这个方法和另一个方法就像它的输出一样:

<input type="hidden" name="ratings1" value="" id="ratings1" />

我已经转储了 $oHidden 变量,它输出:

对象(Zend_Form_Element_Hidden)#143 (29) {
... [“_value”:受保护] =>字符串(7)“评级” [“_view”:受保护] =>无效的 [“_isPartialRendering”:受保护] =>布尔(假) 因此

它暂时设置了该值,但没有渲染它。请让我知道从哪里开始寻找这种行为的原因。

谢谢, 阿列克

I have a quite complex form setup using Zend-Form. At one point I'm setting value of a hidden input using :

$oHidden = new Zend_Form_Element_Hidden('ratings'.$k);        
$oHidden->setValue('ratings');Zend_Debug::dump($oHidden);
$this->addElements(array($oHidden));

This method works well in other places of the same form, but this one, and another one just like i t outputs :

<input type="hidden" name="ratings1" value="" id="ratings1" />

I've dumped the $oHidden variable and it outputs :

object(Zend_Form_Element_Hidden)#143 (29) {
...
["_value":protected] => string(7) "ratings"
["_view":protected] => NULL
["_isPartialRendering":protected] => bool(false)
}

So it sets the value just fine for a while but it doesn't render it. Please let me know where to start looking reasons for this behavior.

Thanks,
Alek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦冥 2024-10-13 13:29:52

问题恰恰出在 isValid() 函数上。它清除表单中的所有值,然后使用传递给它的参数重新填充它。如果参数不存在,它显然不会再出现在表单中,即使它是在前面的几行中明确设置的。

我的案例是登录表单中的可选“重定向”隐藏字段。这是代码(为了便于阅读而进行了简化):

$form = new Form_Login();
$redirect = $this->_getParam('redirect','/user/login/welcome');
$form->addElement('Hidden','redirect',array('value' => $redirect));

if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
    // WTF! the "request" field has no value!!!
}

解决方法是设置操作参数:

$form = new Form_Login();
$redirect = $this->_getParam('redirect','/user/login/welcome');
$this->_setParam('redirect',$redirect);
$form->addElement('Hidden','redirect',array('value' => $redirect));

if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
    // AHA! now it works!
}

我知道这个问题已经有半年了,但是哦,迟到总比不到好:D。

The problem is precisely the isValid() function. It clears all values from the form and then repopulates it with the parametres that are passed to it. If a parametre is absent, it apparently won't appear anymore in the form, even if it was set explicitly a few lines earlier.

My case was an optional "redirect" hidden field in a login form. Here's the code (simplified for readability):

$form = new Form_Login();
$redirect = $this->_getParam('redirect','/user/login/welcome');
$form->addElement('Hidden','redirect',array('value' => $redirect));

if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
    // WTF! the "request" field has no value!!!
}

The workaround was setting the action parametre:

$form = new Form_Login();
$redirect = $this->_getParam('redirect','/user/login/welcome');
$this->_setParam('redirect',$redirect);
$form->addElement('Hidden','redirect',array('value' => $redirect));

if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
    // AHA! now it works!
}

I know the question is half a year old, but oh well, better late than never :D.

贱人配狗天长地久 2024-10-13 13:29:52
$hidden = new Zend_Form_Element_Hidden(array('name' => 'ratings', 'value' => 'ratings'));

试试吧!

$hidden = new Zend_Form_Element_Hidden(array('name' => 'ratings', 'value' => 'ratings'));

Try it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文