vincent
vincent

Reputation: 11

zend_form -- how to get form values except for disables elements

i want to use zend_form to validate and filter the POST data,and some form fields are disabled element, but when i use $form->isValid($post) filtering the data and use $form->getValues() to get the filtered data, it returns all the elements value (including the disabled elements value which i don't want).

such as :

<form method="post" action="">
<input type="text" disabled="disabled" name="account_id" value="123456">

<input type="text"  name="name" value="">
<input type="text"  name="email" value="">

<input type="text" disabled="disabled" name="created_date" value="2011-06-12">
<input type="text" disabled="disabled" name="created_by" value="admin">
<input type="submit">
</form>

so is there any way to get rid of the disables elements value ? (because there are many fields and disabled elements ,so i don't want to trim them manually)

thanks!

Upvotes: 1

Views: 4678

Answers (2)

Francis Yaconiello
Francis Yaconiello

Reputation: 10919

It looks like this is not a bug, but an accepted workflow for validating forms. see this: http://framework.zend.com/issues/browse/ZF-6909

it looks like the accepted solution/trick is to use

$form->isValidPartial($this->getRequest()->getPost())

instead of

$form->isValid($this->getRequest()->getPost())

isValidPartial only tests the form fields that are present in the post. disabled elements should not end up posted.

Upvotes: 2

Hari K T
Hari K T

Reputation: 4244

This is some sort of a hack. We get all elements and iterate through it. When we see an element is disabled we can skip.

$somearray = array();
$elements = $form->getElements();
foreach ($elements as $key => $element) {
    //echo $key;
    if( $element->disabled ) {
        continue;
    }
    $somearray[$key] = $element->getValue();
}

Hope this helps, or you can hack on it ;) .

Upvotes: 4

Related Questions