Zend Framework: isValid () removes values ​​from forbidden form fields!

When you submit a form, disabled form fields are not sent to the request.

So, if your form has a disabled form field, it does the job a Zend_Form::isValid()little frustratingly.

$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 fill out the form again and create duplicate lines of code, what is the best way to approach this problem?

+3
source share
3 answers

Have you disabled an item so that the user cannot edit its contents, but only to view it? If so, just set the element’s readonly attribute to true, I think it will work.

+5
source

, Zend_Form. , isValid :

class Murdej_Form extends Zend_Form {
    function isValid($data) {
        $_data = $this->getValues();
        $valid = parent::isValid($data);
        $this->populate($_data);
        return $valid;
    };
};
+1

instead isValid()we can use isValidPartial(). Unlike isValid(), however, if there is no specific key, it will not perform checks for that particular element. Therefore, isValidPartial()it will not check for disabled fields.

Link: ZF Documentation

0
source

Source: https://habr.com/ru/post/1738390/


All Articles