Multi select dropdown CakePHP 3 selected issue - php

$selected_country = array(1,3);
$this->Form->input('country', [
'options' => $source_types,
'label' => 'Country: ',
'multiple' => true,
'class' => ' form-control',
'selected' => $selected_country,
'type' => 'select'
]);
If selected country has only one value then it selects the option but if the selected country has more than one value then it doesn't select any value.

If you want to pass more than one value on $selected_country, try it:
echo $this->Form->select('rooms', [
'multiple' => true,
// options with values 1 and 3 will be selected as default
'default' => [1, 3]
]);
Reference: CakePHP Cookbook

For anyone this works
echo $this->Form->input('venues._ids', ['options' => $venues, 'class'=>'form-control select4', 'label' => false]); IF you have the database associations set up AND you have it referenced in the controller like so
$event = $this->Events->get($id, [
'contain' => ['Venues']
]);
otherwise adding the 'default' => [1, 2] ids in also works but it has to be first created in the controller and then fed to the view in list form.

Related

How to set default values in Yii2 kartik\tree\TreeViewInput

I used kartik\tree\TreeViewInput in my project but in update form I can't display current selected values in treeview input!
I tried some thing like this based on documentation:
<?= $form->field($model, 'tags')->widget(\kartik\tree\TreeViewInput::className(),[
'name' => 'tags',
'query' => Tags::find()->addOrderBy('root, lft'),
'value' => 1,
'headingOptions' => ['label' => 'tags'],
'rootOptions' => ['label'=>'<i class="fa fa-building"></i>'],
'fontAwesome' => true,
'asDropdown' => true,
'multiple' => true,
'options' => ['disabled' => false]
]); ?>
But it doesn't display tag with (id='1')! How should I display values?
Use:
'displayValue' => 1,
to auto display a node with id = 1 on the form.
If you do not want any node to be auto displayed use:
'displayValue' => 0,

Select2 initial value doesn't appear

I Want to select value base on $_GET if exists soon.
Here is my code in view
<?php
/*if ($model->isNewRecord && !isset($model->agent_id_upper)) {
$model->agent_id_upper = 65;
} not working too*/
//$model->agent_id_upper = 65; ->not working
echo Select2::widget([
'model' => $model,
'name' => 'TbAgent[agent_id_upper]',
//'id' => 'to_id',
'initValueText' => '', // this only for ajax
'data' => $data,
'options' => [
'placeholder' => 'Choose Agent ...',
'multiple' => false,
//'selected' => 65, -> not working
//'value' => 65, -> not working
'class' => ''
],
]
);
?>
65 is ID of record, If selected then it should show username of thus ID.
But my problem is username of 65 is not selected, it only show place holder.
How I can fix this? and Please give me references.
already read this
yii2 select2 by kartik-v set default value
Yii2: Kartik Select2: Initial Value from Model Attribute
http://www.yiiframework.com/forum/index.php/topic/52278-kartik-select2-not-select-corretly/
but no luck with above.
Thanks in advance.
have you tried this? value is not inside options
<?= Select2::widget([
'model' => $model,
'name' => 'id',
'data' => $data,
'value' => 65,
'options' => [
'placeholder' => 'Choose Agent ...',
'multiple' => false,
]
]) ?>

make specific value selected in dropdown fields cakephp 3.0

In select drop down, make one particular value selected from values that are coming in drop down list Cakephp3.0. i am using below code:
$abc = 'india';
echo $this->Form->input('location_id',
['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false]);
Name of countries are coming in drop down list, but i want to make specific value selected which is set as variable $abc (i.e india).
Try this code :
use the 'default' key in
$abc = array('1' => 'One','2'=> 'Two');
echo $this->Form->input('location_id',
'options' => $abc,
default' =>$abc[1],
['empty' =>'Please select',
'required'=>'required',
'class' => 'form-control',
'label'=>false]
);
where $abc[0] is the key of the item you want as selected
like this :
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('field', $options, array('default' => 'F'));
Use value option of form input helper. Like this :
$selected = 'india';
echo $this->Form->input('location_id', ['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false, 'value'=> $selected]);
Suggestion - Read the docs once and then start the development. You will never get stuck at such simple problems. And refer docs first whenever you get stuck at anything.
echo $form->input('field_name', array(
'type' => 'select',
'options' => $arrayOfOptions, // typically set from $this->find('list') in controller
'label'=> 'Label name here',
'value' => $arrProjectLeaderDetails['id'], // specify default value
'escape' => false, // prevent HTML being automatically escaped
'error' => false,
'class' => 'input_select_medium'
));
In cakephp4 :
echo $this->Form->select(
'field_name', [1,2,3,4,5,6],
['empty', => '(Click to select Something']
);
Scroll down a bit from here in the docs: https://book.cakephp.org/4/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls

Yii2 Doesn't Get the Value of Disabled Dropdown List or Textfield

I want to disable certain textfields and dropdown lists to prevent user from changing its values. But whenever I try to, it doesn't collect/get the data of that specific disabled textfield or dropdown list.
Here's my view where I display my dropdown lists. It's inside a for loop:
echo $form->field($model1[$i], 'earning_item_id')->widget(Select2::classname(), [
'data' => $earningslistData,
'options' => ['placeholder' => '', 'prevOptionID' => $model1[$i]->earning_item_id, 'prevOptionName' => $earningslistData[$model1[$i]->earning_item_id],
"name" => "EarningDetails[".$i."][earning_item_id]", "row_count1" => $i],
//'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
'label' => false
]
]);
Here's how it looks like without disabling them:
Then, when I save it, it looks like this:
But, when I disable the dropdown lists, it will give me this:
I think the Full Name comes from my model but I don't know why:
public function getFullName()
{
return $this->user ? $this->user->fname . ' ' . $this->user->lname : 'Full Name';
}
It goes the same when I disable a textfield:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff'],
'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
I am using Kartik widgets for my form fields.
Is there a way to fix this? Please tell me how.
EDIT
Thanks to the commenters below I found out the difference between disabled and readonly. Since it's a dropdown list, here's what I did:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', ],
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', 'style' => 'display:none'],
'pluginOptions' => [
'allowClear' => true,
],
])->label('');
Disabled html form field will not submit, the problem is not with yii itself. The solution in this case is to have 2 copies of the same field, one as disabled as you have already included and the other one hidden with the same value as below after the original one.
echo $form->field($model1[$i], 'earning_item_id')->hiddenInput()->label('');

Yii2 Autocomplete : Save the ID instead of value

I have a list of array called as datalist which contains the name of companies with id.when i use it with the typeahead widget,it captures the value of company with the variable coSearch(id of input).but i want to display the list of companies and when selected,it must give the vale of that id in variable .i am really messed with this problem working from three days.Please help me out.
Here is the code for my activeform which contains the widget.
<?php
$form = ActiveForm::begin([
'action' => ['search'],
'method' => 'get',
]);
$dataList=ArrayHelper::map($companies, 'id', 'name');
echo Typeahead::widget([
'model' => $companySearched,
'name'=>'coSearch',
'options' => ['placeholder' => 'Search company','id'=>'searchCompany1','class' => 'form- control','value'=>'1'],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'local' => $dataList,
'limit' => 10,
]
]
]);
?>
This can be solved with the help of a hidden field.I use autocomplete here
<?php
use yii\web\JsExpression;
use yii\jui\AutoComplete;
$data = Company::find()
->select(['name as value', 'name as label','c_id as id'])
->asArray()
->all();
echo AutoComplete::widget([
'name' => 'Company',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'4',
'select' => new JsExpression("function( event, ui ) {
$('#model-company').val(ui.item.id);
}")],
]);
?>
<?= Html::activeHiddenInput($model, 'company')?>
Hope this help!

Categories