Zend Framework: isValid() clears values from disabled form fields!

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2010-03-24T19:49:06Z Indexed on 2010/03/24 19:53 UTC
Read the original article Hit count: 221

Filed under:
|

When you submit a form, disabled form fields are not submitted in the request.

So if your form has a disabled form field, it makes working with Zend_Form::isValid() a little frustrating.

$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!

Without having to re-populate the form, and create duplicate lines of code, what is the best way to approach this problem?

© Stack Overflow or respective owner

Related posts about zend-framework

Related posts about zend-form