Reputation: 499
I would disable some option's select in Zend Framework 2. I have a select about spoken languages, when the user save a spoken language, I would disable it because he can't save again the same language.
inside LanguageForm.php
$this->add(array(
'name' => 'languages',
'attributes' => array (
'class' => 'form-control',
),
'type' => 'select',
'options' => array(
'label' => 'Languages',
'empty_option' => 'Select spoken languages',
'value_options' => array(
1 => 'English',
2 => 'Spanish',
3 => 'German',
4 => 'Italian'
.......... continue......
),
)));
inside my controller, I tried to do like this, but doesn't work. The function disables the entire select:
$spoken = array (1,2);
$form->get('languages')->setAttribute('disabled', $spoken);
where am I wrong? thanks so much for the help.
Upvotes: 3
Views: 1300
Reputation: 16060
To disable some options you should provide not just a scalar label, but an array:
$options = $form->get('languages')->getValueOptions();
foreach ([1,2] as $value)
{
$options [$value] = [
'label' => $options [$value],
'disabled' => true,
'value' => $value
];
}
$form->get('languages')->setValueOptions($options);
Upvotes: 5