Selected value in a dropdown list Yii2 - yii2

could someone tell me how to create a selected value in dropdown list?
Here is my dropdown list:
<?= Html::dropDownList(
'calculation-type',
$calculateByMonths,
$calculationTypeList, [
'options' => [
Employee::DISABLED =>[
'disabled' => true,
'selection' => true
]
],
'id' => 'calculation-type',
]); ?>
That line selection => true doesn't work, I don't know why :( Thanks for the help.

As you can see in official Yii2 documentation second argument in Html::dropDownList is $selection, and it has to contain string or array of selected values.
Values in dropDownList are keys in $items array. For example, if you have an array of months and you need to make February selected:
<?php
$month = [
'jan' => 'January',
'feb' => 'February',
'mar' => 'March',
'apr' => 'April',
];
echo \yii\helpers\Html::dropDownList(
'months', //name of your select tag
'feb', // selected option value
$month // array of items
);
?>
<!-- Output of the dropDownList-->
<select name="months">
<option value="jan">January</option>
<option value="feb" selected>February</option>
<option value="mar">March</option>
<option value="apr">April</option>
</select>

Related

CakePHP 3.7.1 Add Classes to Form Select Control Options

From the documentation, I have been able to build a Form Select Control, However, It is unclear to me how to add classes to the select options...
Here is my select control in CakePHP Form
<?= $this->Form->select('type', [
'Value 1' => 'Name 1',
'Value 2' => 'Name 2'
],[
'class' => 'js-custom-select w-100 u-select-v2 u-shadow-v19 g-brd-none g-color-black g-color-primary--hover g-bg-white text-left g-rounded-30 g-pl-30 g-py-12',
'data-open-icon' => "fa fa-angle-down",
'data-close-icon' => "fa fa-angle-up"
]
) ?>
Here is my select control in HTML
<select name="type" class="js-custom-select w-100 u-select-v2 u-shadow-v19 g-brd-none g-color-black g-color-primary--hover g-bg-white text-left g-rounded-30 g-pl-30 g-py-12" data-placeholder="Type" data-open-icon="fa fa-angle-down" data-close-icon="fa fa-angle-up">
<option class="g-brd-secondary-light-v2 g-color-black g-color-white--active g-bg-primary--active" value="value 1">Name 1</option>
<option class="g-brd-secondary-light-v2 g-color-black g-color-white--active g-bg-primary--active" value="value 2">Name 2</option>
</select>
How do I get the classes that are on the options into the CakePHP Form of the control?
To add classes to your options, you need to use specific options array structure:
$options = [
[
"text" => "Text to display for option 1",
"value" => "Value to set for option 1",
"class" => "Class list to set for option 1"
],
[
"text" => "Text to display for option 2",
"value" => "Value to set for option 2",
"class" => "Class list to set for option 2"
],
/** ... **/
]
After preparing such array, you can use it with FormHelper::select() or FormHelper::control():
$this->Form->select("field_name",$options);
$this->Form->control("field_name",[
"label" => "My Label",
"type" => "select",
"options" => $options
]);

Yii2 add extra attribute in select > options list

How I can add an extra attribute like in below screenshot state_id=1 in options list for all.
<?= $form->field($model, 'district_id')->dropDownList(ArrayHelper::map($Districts, 'id', 'name')) ?>
You need to iterate through the $Districts array and associate all the attributes you want to add to the <option> of the dropdown, i assume that your $Districts array has something like below
$Districts=[
1=>"North Andaman",
2=>"South Andaman"
3=>"Nicobar"
];
Now you need to iterate that array and associate the attributes with every option
foreach ($Districts as $id => $name) {
$optionAttributes[$id] = ['my-attr' => 'value'];
}
The above will show you something like
Array
(
[1] => Array
(
[my-attr] => value
)
[2] => Array
(
[my-attr] => value
)
[3] => Array
(
[my-attr] => value
)
)
Now when creating your dropdown you should pass this array to the options option of the dropdownList() see below
echo $form->field($model, 'district_id')->dropDownList(
$Districts,
['options' => $optionAttributes]
);
Now if you see the source of the page it will show you the dropdown like below
<select id="contacts-district_id" name="Contacts[district_id]" class="form-control">
<option value="1" my-attr="value">North Andaman</option>
<option value="2" my-attr="value">South Andaman</option>
<option value="3" my-attr="value">Nicobar</option>
</select>

yii2 ActiveForm numeric textfield

I've created an ActiveForm using yii2 like this:
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
// ** i want numeric value **
])->label(false)?>
and it rendered a result of:
<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label><input type="text" id="item-finalprice" class="form-control" name="Item[finalPrice]"><p class="help-block help-block-error"></p></span>
now i want to make it < input type="number" .. and not text.. (so user could change value using browser up/down buttons). is there any way to do it?
You can use ->textInput(['type' => 'number'] eg :
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
'type' => 'number'
])->label(false)?>
Try this . it worked for me
<?= $form->field($model, 'amount')->textInput(['type' => 'number']) ?>
Field like Phone Number/Membership No etc, some time we allow user only to enter numeric input in a text field. In such case applying pattern match rule work great for me.
Simply set a rule in the model class and you are done.
public function rules()
{
return [
...
[['contactno'], 'string', 'max' => 25],
[['contactno'], 'match' ,'pattern'=>'/^[0-9]+$/u', 'message'=> 'Contact No can Contain only numeric characters.'],
...
];
}
<?= $form->field($model, 'code')->textInput(['type'=>'number']) ?>

SELECT element option values

i am doing a select in html then I have something in the JS part like this
var languageEN = "en,1";
var languageFR = "fr,2";
var languageDE = "de,3";
and my html markup will be like
<select style="display: none;" id="select">
<option value=languageEN>English</option>
<option value=languageFR>Francais</option>
<option value=languageDE>Deutsch</option>
</select>
but it seems that I am hitting undefined.
I been trying to find whats the correct syntax to include in the option value but no avail. thanks.
If using PHP is an option you could do this. The first part could go at the top of the page, then the second part wherever the select needed to be. You could add as many options or change them any way you liked this way.
<?php
$options = array(
0 => array(
'name' => 'English',
'value' => 'languageEN'
),
1 => array(
'name' => 'Francais',
'value' => 'languageFR'
),
2 => array(
'name' => 'Deutsch',
'value' => 'languageDE'
),
);?>
<select style="display: none;" id="select">
<?php
foreach ($options as $option);
{
echo '<option value="'.$option['value'].'">".$option['name'].'</option>'
}
?>
</select>

CakePHP onChange event

I have no idea why this onChange event is not working. Maybe I got so used to the code that I can't see my mistake. I'd appreciate your help:
<td class="cellInput">
<?php
$options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
$attributes = array('legend' => false);
echo $form->select('capacidade', $options, array(
'class' => '',
'label' => '',
'default' => '200',
'onchange' => "javascript:checkForOther(this);",
));
?>
</td>
As others suggested, place the attributes in the correct parameter position. The other thing i would do is remove the javascript and just have the function name in there like:
From: 'onchange' => "javascript:checkForOther(this);"
To: 'onchange' => "checkForOther(this)"
Try
Putting the attributes as the fourth argument instead of the third:
echo $form->select('capacidade', $options, null,
array(
'class'=>''
,'label' => ''
,'default' => '200'
,'onchange' => "javascript:checkForOther(this);"
)
);
Does the source of the generated page have the onChange attribute?
See the documentation
According to CakePHP documentation on select method, the third argument is used to choose which option is selected by default.
You must use the fourth argument to pass HTML attributes to a select element:
<td class="cellInput">
<?php
$options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
$attributes = array('legend'=>false);
echo $form->select('capacidade', $options, '200', array('class'=> '', 'label' => '', 'onchange' => "checkForOther(this);"));
?>
</td>