views:

1700

answers:

4

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.

+2  A: 
$element->setAttrib('readonly', 'true');

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

vartec
+2  A: 

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.

Amr Mostafa
What I'm thinking of doing is taking this a step further and creating a new custom element instead of just a decorator. I'll let you know how I go.
Joel Lignier
+2  A: 

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.

Joel Lignier
+1  A: 

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().

Dessislava Mitova
Thanks for the info but as far as I can see using the formNote view helper is still simpler. Using this gave me text directly in the form without having to use a disabled text input or custom elements.
Joel Lignier