Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set readonly property in zend form add element

Hi I am new in zend framework. I want to set ready only property on input box in zend Form. example as we do in html

<input type ="text" readonly="readonly" />  

this is my zend code:

$this->addElement('text', 'name', array(
            'label'      => '',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'style'    => array('width:338px'),
             'autocomplete' => 'off',
            'decorators'=>Array(
            'ViewHelper',
            'Errors',

           ),

help mee

like image 227
Delta Avatar asked Jun 05 '13 06:06

Delta


3 Answers

Try this

$this->getElement('text')->setAttrib('readonly', 'readonly');
like image 159
Kiren S Avatar answered Sep 27 '22 22:09

Kiren S


Try something like this:

$this->addElement('text','text_field',array('attribs' => array('readonly' => 'true'))); 
like image 41
Moldovan Daniel Avatar answered Sep 28 '22 00:09

Moldovan Daniel


In ZF2 you can create a form by extending Zend\Form and then add form element in the constructor. there you can set the attributes as follows.

use Zend\Form\Form;

class MyForm extends Form {
    public function __construct() {

        $this->add(array(
            'name' => 'name',
            'type' => 'Text',
            'attributes' => array(
                'id' => 'name',
                'class' => 'form-control',
                'readonly' => TRUE,
            ),
            'options' => array(
                'label' => 'Name : '
            )
        ));
     }
}
like image 39
Ruwantha Avatar answered Sep 28 '22 00:09

Ruwantha