如何禁用 Zend_Form_Element_Radio 但仍将其值包含在 POST 请求中?
问候各位编码员。
在我的应用程序中,我有 Zend_Forms 和几个 Zend_Form_Elements。根据用户的角色,我想将其中一些元素更改为只读/禁用状态。然而,重要的是提交表格仍然有效。 POST 应该为每个元素发送一个值。 因此,设置 readonly 属性可以正常工作,但禁用属性则不行。如果禁用 HTML 表单元素,它不会在 POST 请求中传递其值。
问题是,不可能在所有类型的表单元素上设置只读属性。它对单选按钮没有影响。
/**
*
* changes the Zend_Form_Element to a 'readonly' like state
*
* @param String $name (zend-) name of the element
*/
public function readonlyElement($name){
$elem = $this->getForm()->getElement($name);
/* @var $elem Zend_Form_Element */
$attribs = $elem->getAttribs();
if(is_a($elem, 'Zend_Form_Element_Radio')){
// !! this does NOT work !!
$attribs['disabled'] = true;
$value = $elem->getValue();
$hidden = new Zend_Form_Element_Hidden($name);
$hidden->setValue($value);
$this->getForm()->addElement($hidden);
}
else {
$attribs['readonly'] = true;
}
$style = $attribs['style'];
$attribs['style'] = $style;
$elem->setAttribs($attribs);
}
我尝试禁用单选按钮并添加具有相同值和相同名称的隐藏字段。这不起作用,因为 Zend_Form_Element 名称必须是唯一的。我还使用了 $elem->setName(),将现有元素的名称更改为其他名称。到目前为止,我还没有成功。您对如何解决我的问题有什么想法吗?
谢谢,
安德烈亚斯
澄清一下: 这就是我在控制器中使用表单的方式:
$form = new Some_Form();
//attach filters, validators, remove unneeded elements based on active user
$this->modifyForm($form);
if ($this->getRequest->save){
if $form->isValid(($this->getRequest->params){
//... Check and save
}
}
else{
$form->populate($model->toArray());
}
首先填充“禁用”字段,但如果表单验证失败,它们将为空,因为没有发送 POST 数据。添加与禁用字段同名的隐藏字段可以在纯 PHP/HTML 中解决此问题。
Greetings fellow coders.
In my application I have Zend_Forms with several Zend_Form_Elements. Based on the user' role I'd like to change some of these elements to a readonly/disabled status. However, it is important that submitting the form still works. POST should send a value for each element.
So setting the readonly attribute works fine, but disabled does not. If you disable a HTML form element, it does not pass its value in the POST request.
The problem is, that it is not possible to set the readonly attribute on all types of form elements. It has no effect on radio buttons.
/**
*
* changes the Zend_Form_Element to a 'readonly' like state
*
* @param String $name (zend-) name of the element
*/
public function readonlyElement($name){
$elem = $this->getForm()->getElement($name);
/* @var $elem Zend_Form_Element */
$attribs = $elem->getAttribs();
if(is_a($elem, 'Zend_Form_Element_Radio')){
// !! this does NOT work !!
$attribs['disabled'] = true;
$value = $elem->getValue();
$hidden = new Zend_Form_Element_Hidden($name);
$hidden->setValue($value);
$this->getForm()->addElement($hidden);
}
else {
$attribs['readonly'] = true;
}
$style = $attribs['style'];
$attribs['style'] = $style;
$elem->setAttribs($attribs);
}
I tried to disable the radio button and add a hidden field with the same value and the same name. This did not work, because Zend_Form_Element names have to be unique. I also played around with $elem->setName(), to change the name of the existing element to something else. So far, I was not successful. Do you have any ideas how to solve my problem?
Thanks,
Andreas
To clarify:
This is how I use my forms in a controller:
$form = new Some_Form();
//attach filters, validators, remove unneeded elements based on active user
$this->modifyForm($form);
if ($this->getRequest->save){
if $form->isValid(($this->getRequest->params){
//... Check and save
}
}
else{
$form->populate($model->toArray());
}
'Disabled' fields will be populated first, but if form validation fails they will be empty because no POST data was send. Adding a hidden field with the same name as the disabled field would solve this problem in pure PHP/HTML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
始终验证模型内的值! 不要仅依赖您的表单值!对于任何拥有现代浏览器的人来说,更改只读输入的值都是小菜一碟。
所以基本上在你的模型中做类似的事情
在任何情况下你都不应该依赖表单发送的任何内容。显然,当像这样使用 _getParam('value') 时,应该有某种验证器检查发送的 id(我猜这就是您从无线电元素中选择的)是否存在于数据库中(Db_RecordExists )
Always validate Values inside of your Model! Do not rely on your Form-Values alone! It is a piece of cake for anyone with a modern Browser to change the value of a readonly input.
SO basically do something like this inside your Model
Under no circumstances you should rely on anything the form sends. Obviously when using
_getParam('value')
like this there should be some sort of validator checking if the sent id (i guess that's what you're choosing form your radio element) exists in the database (Db_RecordExists)