只读属性未应用于选择框。 这是我的表单代码:
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 代码:
$form->get('role')->setAttribute('readonly', 'readonly');
在我的 Controller 中,我在选择框上应用了只读属性,但未应用只读属性我如何应用只读属性?
最佳答案
选择没有只读属性。这是关于 w3c 的选择描述
您可以做的是禁用选择并添加一个隐藏字段(如果您希望传递默认值)
例子:
$form->get('field_name')->setAttribute('disabled', 'disabled');
或者使用工厂方法:
$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',
),
));
关于zend-framework2 - 如何在 zf2 中的选择字段上应用只读属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162306/