Is it possible to disable the options in a select element?
I have a form with a select element that by default has a lot of options available. During form creation, depending on information retrieved from the database, i would like to disable certain options.
Some research came up with
$form->get('selectElement')->setAttribute("disabled", array(0, 1, 2));
...which should disable the first 3 options, but unfortunately does not.
You must use the setAttribute() method to set the attributes of your select element, not its options. For this, you should use setValueOptions():
$myOptions = $form->get('selectElement')->getValueOptions();
foreach ([0, 1, 2] as $value) {
$myOptions [$value]['disabled'] = true ;
}
$form->get('selectElement')->setValueOptions($myOptions);
$myOptionsmust be an array of options:
[
[
'label' => 'My first option',
'disabled' => false,
'value' => 1
],
[
'label' => '2nd option',
'disabled' => false,
'value' => 2
],
[
'label' => '3rd option disabled',
'disabled' => true,
'value' => 3
],
]
Related
In a Fieldset I have an Element\Radio foo and Element\Text bar.
public function init()
{
$this->add(
[
'type' => 'radio',
'name' => 'foo',
'options' => [
'label' => _('foo'),
'value_options' => [
[
'value' => 'a',
'label' => 'a',
'selected' => true
],
[
'value' => 'b',
'label' => 'b'
]
]
]
...
]);
$this->add(
[
'name' => 'bar',
'type' => 'text',
'options' => [
'label' => 'bar',
...
],
...
]);
}
The validation of the field bar is depending on the selected foo option. It's easy to implement, if I can get the selected value of foo:
public function getInputFilterSpecification()
{
return [
'bar' => [
'required' => $this->get('foo')->getCheckedValue() === 'a',
...
],
];
}
But there is no method Radio#getCheckedValue(). Well, I can iterate over the $this->get('foo')->getOptions()['value_options'], but is it really the only way?
How to get (in the Fieldset#getInputFilterSpecification()) the selected option of a Zend\Form\Element\Radio?
The selected option gets POSTed to the server along with everything else from the HTML form and is all of this is available in validators through the $context array.
You can create a conditionally required field by using a callback validator and the $context array like this:
public function getInputFilterSpecification() {
return [
'bar' => [
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
'required' => true,
'validators' => [
[
'name' => 'Callback',
'options' => [
'callback' => function ($value, $context) {
return $context['foo'] === 'a'
},
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'This value is required when selecting "a".'
]
]
]
]
],
];
}
That would check if 'foo' is equal to 'a', i.e. option 'a' is selected and return true when it is, which marks the input as valid, and false when it's not, marking the input invalid.
I'm using Zend\Form\Element\MultiCheckbox with Zend\Form\View\Helper\FormMultiCheckbox:
MyFieldset.php
// namespace ...;
// use ....;
class MyFieldset extends Fieldset
{
// ...
public function init()
{
parent::init();
$this->add(
[
'type' => 'multi_checkbox',
'name' => 'mymulticheckbox',
'options' => [
'label' => _('global label'),
'label_attributes' => [
'class' => 'col-md-3',
],
'value_options' => [
[
'value' => 'foo',
'label' => 'FOO',
],
[
'value' => 'bar',
'label' => 'BAR',
],
[
'value' => 'buz',
'label' => 'BUZ',
],
]
],
]
);
}
// ...
}
myform.phml
use Zend\Form\View\Helper\FormMultiCheckbox;
echo $this->formMultiCheckbox($myFieldset->get('mymulticheckbox'), FormMultiCheckbox::LABEL_PREPEND);
It works, but the "global label" is not displayed. It gets displayed, when I'm using Zend\Form\View\Helper\FormElement, but the FormMultiCheckbox seems to ignore the "global label".
How to make FormMultiCheckbox display the label of the checkbox list?
Have you tried with formRow(). For me it works. This does not appear to be managed in formMultiCheckbox(). See lines 182-193, file zend-form/src/View/Helper/FormRow.php.
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox'
|| $type === 'radio'
|| $element instanceof MonthSelect
|| $element instanceof Captcha
) {
$markup = sprintf(
'<fieldset><legend>%s</legend>%s</fieldset>',
$label,
$elementString
);
How do I hide select all on select2?
https://select2.github.io/options.html
http://demos.krajee.com/widget-details/select2
'showToggleAll' => false, // does not work
you might want to find out why it does not work sense the demo version works ok but if you need to hide it...
$(".s2-togall-button, .s2-togall-select").css("display", "none")
run after the select2 has loaded seem to work for me.
Correct use of that option is like next:
echo $form->field($model, 'attribute')->widget(
Select2::classname(),
[
'data' => $arrayValues,
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'class' => 'form-control',
'prompt'=> 'Select up to 3 values',
'multiple' => true,
],
'pluginOptions' => [
'tags' => false,
'maximumSelectionLength' => 3,
'allowClear' => false,
//'tokenSeparators' => [',', ' '],
'closeOnSelect' => true,
],
'showToggleAll' => false,
]
);
I hope will be useful
I want to change url in action view, from view to viewdaily, viewweekly and viewmonthly, what should i do?
[
'class' => 'yii\grid\ActionColumn',
],
The simplest way is not create/use ActionColumn, instead you can use raw-column and show there what you want:
[
'attribute' => 'some_title',
'format' => 'raw',
'value' => function ($model) {
return 'your Text';
},
],
I have form with collection. And i attach validation to whole collecion - i just want to check the existence of certain relations between elements of the collection.
And it works great. in the case of wrong data - form does not pass the "isValid()" test.
But there is one problem. formElementErrors / getMessages didnt return anything.
What i do wrong?
My form:
class Form implements InputFilterProviderInterface {
/**
* #return array
*/
public function getInputFilterSpecification()
{
return [
[
'name' => 'legend',
'required' => true,
'allowEmpty' => false,
'validators' => [
['name' => 'Callback', 'options' => [
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'Wrong',
],
'callback' => function ($values, $context=[]) {
return false;
},
]],
]
],
];
}
public function init()
{
$this->add(
[
'name' => 'legend',
'type' => 'Zend\Form\Element\Collection',
'options' => [
'label' => 'Legenda',
'count' => 2,
'should_create_template' => true,
'allow_add' => true,
'template_placeholder' => '__placeholder__',
'target_element' => [
'type' => 'Narzedzie\Form\Legenda\LegendyOpcjeFieldset',
],
],
]
);
}
}
And view:
$element = $NarzedzieForm->get('legend');
var_dump($element->getMessages()); // in case of error - empty array!
echo $this->formElementErrors($element); // in case of error - empty string
echo $this->formColleciton($element);
Maybe you need to add both messages?
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'Wrong VALUE',
\Zend\Validator\Callback::INVALID_CALLBACK => 'Wrong CALLBACK',
],
as perhaps the invalid callback message is being suppressed as you are only supplying one? I would hope it would fall back to the default. But then all this validator message stuff seems a bit stupid to me the way it is done.
Looks like you have an error in your callback, which might be throwing an exception and is being caught in the validator in the try catch statement maybe?
should be?
function ($values, $context=[]) {
foreach ($values as $value) {
if ($value['el'] == '1') return false;
}
return true;
},
as in $values not $value for the array in the foreach? Probably want to check that key is set too, with an isset($value['el'])?