问题描述:

readonly property not applied on selectbox.

here is my code of form:

array(

'type' => 'Select',

'name' => 'role',

'attributes' => array(

'id' => 'role',

'error_msg' => 'Select User Role',

),

'options' => array(

'label' => 'Role'

),

'validation' => array(

'required'=>false,

'filters'=> array(

array('name'=>'StripTags'),

array('name'=>'StringTrim')

),

'validators'=>array(

array(

'name'=>'StringLength',

'options'=>array(

'encoding'=>'UTF-8',

'min'=>1,

'max'=>250

)

)

)

)

),

controller code:

 $form->get('role')->setAttribute('readonly', 'readonly');

and in my controller i apply readonly property on selectbox but readonly property not applied how i apply readonly property?

网友答案:

Select don't have the readonly property. Here is the select description on w3c

What you can do is disable the select and add a hidden field (if you wish to pass a default value)

Example:

$form->get('field_name')->setAttribute('disabled', 'disabled');

or using the factory method:

$this->add(array(
    'name' => 'field_name',
    'type' => 'Zend\Form\Element\Select',
    'options' => array(
        'label' => 'Type',
        'value_options' => array(
            'val1'  => 'name1',
            'valN'      => 'nameN'
        ),
    ),
    'attributes' => array(
        'disabled' => 'disabled',
    ),
));
网友答案:

I had the same problem and my solution was:

  1. I put the form element select to disabled.
  2. I added the Form element into a formHidden and FormSelect.

Example in view phtml file:

$element = $form->get('name');
if ($isReadOnly) {
   echo $this->formHidden($element);
   $element->setAttribute('disabled', 'disabled');
   echo $this->formSelect($element);  //Display the select to user
} 

Hope it helps.

相关阅读:
Top