Our blog

 

Zend Form Disable Fields

I use this little function as part of my base form class to enable me to easily lock certain form elements.

PHP:
  1. public function lockField($field){       
  2.         $elem = $this->getElement($field);       
  3.         $elem->setAttrib('disabled', true);
  4.         $elem->setAttrib('readonly',true);
  5.     }

usage might be like the following:

PHP:
  1. public function makeForm($action){
  2.     $form = new My_Form(array('action'=>$action));
  3.     $form->addElement(.. custom form add element function);
  4.     $form->lockField('fieldname');
  5.     $form->addSubmit(.. custom add submit function);
  6.     return $form;
  7. }

More Reading:

3 Comments

Jeremy
September 9th, 2009

How could you attach this functionality to a check box? Eg. You have a form with check boxes. When you click a check box you want it to display a text box element.

An example is where you are trying to find out how a customer came to your page.
They click the 'other - please specify' check box and the text box appears so they can enter text.

 

admin
September 9th, 2009

I think you would be best approaching that with javascript.

you could use this function to lock the textbox and then use javascript to unlock and hide it.

 

SousGarden
April 15th, 2010
 

 

Leave a Reply