【发布时间】:2010-03-24 19:49:06
【问题描述】:
当您提交表单时,请求中不会提交禁用的表单字段。
因此,如果您的表单有一个禁用的表单字段,那么使用 Zend_Form::isValid() 会有点令人沮丧。
$form->populate($originalData);
$form->my_text_field->disabled = 'disabled';
if (!$form->isValid($_POST)) {
//form is not valid
//since my_text_field is disabled, it doesn't get submitted in the request
//isValid() will clear the disabled field value, so now we have to re-populate the field
$form->my_text_field->value($originalData['my_text_field']);
$this->view->form = $form;
return;
}
// if the form is valid, and we call $form->getValues() to save the data, our disabled field value has been cleared!
无需重新填充表单并创建重复的代码行,解决此问题的最佳方法是什么?
【问题讨论】:
-
这并不取决于 Zend Form; HTML 规范说禁用元素不会被提交,而只读元素会提交(这解释了下面@robertbasic 的建议)。