简体   繁体   中英

Zend Framework - Static form elements

I have got a form which a user can use to create a new store and to edit an existing one. When this form is being used to edit a store there are certain fields that I want the user to see but not edit eg. store_id. I have explored the different Zend_Form_Elements hoping to find some kind of static element but with no luck.

So my question is, how do I display information using Zend_Form that a user can't edit?

Thanks.

readonly alone is not enough, because users will still be able to edit it if they really want. You should use $element->setIgnore(true) that will ensure that Zend_Form_Element won't try to populate the element from POST/GET, and I'd double check that also. You have to make sure the values you are getting into the databases can never contain this element.

Finally, if you would like your element to be displayed in a different way than just with readonly , you can do that by changing the element decorators.

I just managed to work this one out myself. The solution was to change the view helper on the elements to the formNote helper eg. $element->helper = 'formNote'. The result of this was that the value gets displayed as straight text instead of being inside a form element.

Thanks for your answers.

That's very good solution when you don't need to populate the element value when the form is submitted. It's equivalent solution is to use the Form Element method setAttrib() and disable the form element

$formElement->setAttrib('disable','disable')  

which will only freeze the element.

But if you need to populate the field, using the previous solutions you will probably need additional hidden field added, which will pass the value. Developing custom form element will be good style but that's not welcomed by each developer so you can use some tricky way to set a form element as a text only but populate its value. That way is when you create the element as a hidden field, set its value and use the Form Element method setDescription() to set and display the element text value.

$formElement = new Zend_Form_Element_Hidden( 'elName',
        array( 'label' => 'elLabel', 'value' => 'elValue' ) );

$formElement->setDescription( 'elValue' );

Then you can render that hidden element and display the value with the $formElement->getDescription() .

$element->setAttrib('readonly', 'true');

http://www.w3.org/TR/html401/interact/forms.html#adef-readonly

According to Amr Mostafa, if you use:

$element->setAttrib('readonly', 'true');

OR

$element->setAttribs(array('disabled' => 'disabled'));

User still send values by POST/GET and they are stored in DB.

The only way for me to don't taking into account the values from POST/GES is:

$element->setIgnore(true)

Example:

$element = new Zend_Form_Element_Text('element');
$element->setIgnore(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM