Yii2 kartik-v select2 widget allowClear and placeholder not working properly - php

I'm using the advanced template of Yii2. Whenever the select2 'placeholder' is assigned a value, the widget won't show the initial value. I can work around this by using ['placeholder' => $placeholder] and setting the variable to NULL whenever an initial value exists. However, if the placeholder is NULL and an initial value is showing, then allowClear doesn't work. What am I doing wrong?
View:
use kartik\select2\Select2;
<?= $form->field($profile, 'associationSelect')->widget(Select2::classname(), [
'data' => $associationList,
'language' => 'en',
'theme' => 'krajee',
'options' => ['placeholder' => 'Select Association ...'],
'pluginOptions' => ['allowClear' => true],
])->label(''); ?>
Controller:
$associationArray = Association::find()
->select('association, id')
->indexBy('id')
->orderBy('association')
->all();
$associationList = ArrayHelper::map($associationArray, 'id', 'association');
$associationList is an array like {[1]=>"association1", [2]=>"association2, [3] ..."}.
$profile->associationSelect is prepopulated by the controller with a key. My problem is that the widget always shows 'Select Association ...' whether $profile->associationSelect is populated or not. I only want the placeholder to show if $associationSelect is empty, as that's how a placeholder is supposed to work. And if I leave placeholder empty (NULL), the clear option ("x") shows up in the widget, but won't clear the text (i.e. it doesn't do anything when clicked).

I either didn't completely understand your question or I think I have solved your problem with only a few changes. I have put here several different things so you might not need some of them.
/*
* $profile->associationSelect: data list and assigned default value by key: 2 (third element).
* With this line, your select2 will have a default chosen value from data list.
* Remove this (1) line to show placeholder instead.
*/
if (!empty($associationList)) {
$form->associationSelect = 2;
echo $form->field($profile, 'associationSelect')->widget(Select2::classname(), [
'data' => $associationList,
'language' => 'en',
'theme' => 'krajee',
'options' => [
'placeholder' => $associationList[3] // Placeholder will show 4th element from data list (we do have a list)
],
'pluginOptions' => [
'allowClear' => true
],
]);
} else {
echo $form->field($profile, 'associationSelect')->widget(Select2::classname(), [
'data' => $associationList,
'language' => 'en',
'theme' => 'krajee',
'options' => [
'placeholder' => 'Please select a value' // Placeholder will be default text since we don't have a list.
],
'pluginOptions' => [
'allowClear' => true
],
]);
}
You may choose which option you want (whether with default selected value or just with placeholder). In both cases you can use allowClear and you won't get any errors (if done correctly, of course).

The problem is that 'associationSelect' is an internal attribute rather than a db attribute. I switched to a db attribute and it works just fine.
<?= $form->field($profile, 'association')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Association::find()->orderBy('id')->all(), 'id', 'association'),
'language' => 'en',
'theme' => 'krajee',
'options' => ['placeholder' => 'Select ...',],
'pluginOptions' => ['allowClear' => true],
])->label(''); ?>

Related

yii2 grid view filter text field content auto-selected

In the grid view (Kartik) I would like to make any text in the text field of the filter (from previous filtering) to be selected as if I had double clicked it with mouse. So I can type a new filtering without the need to double click the field first to be able to immediately overwrite the previous value. I've searched, found nothing, tried intuitively like this, don't work:
[
'attribute' => 'numbern',
'filterInputOptions' => [
'class' => 'form-control',
'autofocus' => true,
'selected' => true,
],
],
Can you please point me into the right direction? Thanks!
Maybe some extra javascript like this (not tested)...
[
'attribute' => 'numbern',
'filterInputOptions' => [
'class' => 'form-control',
'autofocus' => true,
'onFocus' => 'this.select()',
],
],

Setting value of yii2 select2 widget from javascript

Is it possible to set certain options as selected in yii2 select2 widget from jquery. The widget is not initialised through js but it is bound to model attribute in form. Any help would be appreciated
Code:
echo $form->field($model, 'news_tags')->widget(Select2::classname(), [
'data' => ArrayHelper::map(app\models\Topics::findAll(['status' => 1]), 'tp_id', 'title'),
'options' => ['multiple' => true, 'placeholder' => 'Select Tags ...'],
'pluginOptions' => [
'tags' => false,
'allowClear' => true
],
])->label(FALSE);
I am trying to use an ajax call and on succes i want to add some tags from clientside.
As is presented in docs https://select2.github.io/examples.html#programmatic
you can use following code:
$("#form-field").val("5").trigger("change");
and it will select option, that has id 5

How to perform Filter using Multi Select Dropdown in Yii2 GridView

Here i like to explain my problem clearly,
am trying to perform multi select dropdown filter, before this multiselect filter i have a basic filter.
Am using kartik-v dropdown extension
search.php
<?php
$status = ArrayHelper::map(Status::find()->all(),'id','status');
echo $form->field($model, 'status')->widget(Select2::classname(), [
'data' => $status,
'language' => 'en',
'options' => [
'placeholder' => 'Select Status..',
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
claimsSearch.php
$query->andFilterWhere([
'status' => $this->status
]);
if i try the above code am getting error as below
Array to string conversion
but here i don't know how to write filter code.
update searchview:
Try to delete 'status' from EmployeeSearch rules.
You cannot filter such field automatic way.
Or you must set up custom filter value for status column, like this (you can dig into this direction):
How can I use a simple Dropdown list in the search box of GridView::widget, Yii2? Try this link
You are not calling the model in that widget. You shoudl use like this:
echo $form->field($mySearchModel, 'state_10')->widget(Select2::classname(), [
'data' => $status,
'options' => [
'placeholder' => 'Select Status ...',
'multiple' => true
],
]);
And your select it's probably returning an array. So, your search would be something like:
$query->andFilterWhere([
'status' => ('in', 'status', $this->status)
]);
See more examples of queries here.
If that solution don't work, i will sugest you to do a var_dump($yourModel->status) in your view, just to check what is returning.
$this->status is array?
So, you can use
<?php
$status = ArrayHelper::map(Status::::model()->findAllByAttributes(array("id"=>$status));(),'id','status');
echo $form->field($model, 'status')->widget(Select2::classname(), [
'data' => $status,
'language' => 'en',
'options' => [
'placeholder' => 'Select Status..',
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>

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('');

ActiveForm, custom id for field broke validation

I'm pretty new with Yii2 and have the next unpleasant issue.
I created two forms at same page like
<?php $form = ActiveForm::begin([
// make sure you set the id of the form
'id' => 'create',
'action' => $action,
'options' => ['enctype' => 'multipart/form-data']
]); ?>
<?php $form = ActiveForm::begin([
// make sure you set the id of the form
'id' => 'update',
'action' => $action,
'options' => ['enctype' => 'multipart/form-data']
]); ?>
I use the same model for both form fields like
<?= $form->field($action_model, 'action_name',[
'addon' => ['prepend' => ['content'=>$action_model->getAttributeLabel('action_mobile')]]
])->widget(Typeahead::classname(), [
'options' => ['placeholder' => $action_model->getAttributeLabel('action_placeholder')],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'local' => Json::encode( $totalLookUp['action_lookUp'] ),
'limit' => 10
]
]
])->label(false);
?>
And here is the issue. In that case I have two forms with same scope of fields, with same names and same id. What for sure will be not valid for W3C. The another one issue that inspite of that fact that client side presubmit javascript validation for both forms work perfect. The typeahed widget works only for first set of fields since it bindid by id.
If i try to override elements id by specify it by widgets options like
<?= $form->field($action_model, 'action_name',[
'addon' => ['prepend' => ['content'=>$action_model->getAttributeLabel('action_mobile')]]
])->widget(Typeahead::classname(), [
'options' => ['id'=> $form_id.'_action_name',
'placeholder' => $action_model->getAttributeLabel('action_placeholder')],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'local' => Json::encode( $totalLookUp['action_lookUp'] ),
'limit' => 10
]
]
])->label(false);
?>
The typeahead works perfect, but in that case validation fails, I mean it just stop working.
So the questuion is, how to make possible adjust validation script and use unique form id's.
From the docs:
If you set a custom id for the input element, you may need to adjust the $selectors accordingly.
For your case:
$form->field($action_model, 'action_name', [
'selectors' => ['input' => '#'.$form_id.'_action_name'],
'addon' => ...

Categories