Zend Framework:isValid()清除禁用表单字段中的值!

And*_*rew 3 zend-framework zend-form

提交表单时,请求中不会提交已禁用的表单字段.

因此,如果您的表单具有禁用的表单字段,那么它会让您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!
Run Code Online (Sandbox Code Playgroud)

无需重新填充表单,并创建重复的代码行,解决此问题的最佳方法是什么?

小智 5

您是否将元素设置为禁用,以便用户无法编辑其内容但只能查看它?如果是这样,只需将元素的readonly属性设置为true,我认为它将以这种方式工作.