How to make a placeholder in a listBox - yii2

It is necessary that in the field listBoxt, there was a placeholder, something like "Select a role for the user", i.e. message that the user sees without selecting anything in the listbox. But this should include the option 'prompt' => 'remove the role' so that the user can remove the role through the drop-down list.
Is it possible to do this through the standard Yii functional without resorting to JS?
echo $form->field($model, 'additionalRoles', [
'options' => [
'class' => 'form-group',
],
])->listBox($additionalRoles, [
'class' => 'form-control j-multi-select2',
'prompt' => 'Select additional role',
]);
For example: https://jsfiddle.net/8e7avn2d/1/

<?=
$form->field($model, 'additionalRoles', [
'options' => [
'class' => 'form-group',
],
])
->listBox($additionalRoles, [
'class' => 'form-control j-multi-select2',
'prompt' => [
'text' => "Select additional role",
'options' => [
'disabled' => true,
'selected' => true,
'hidden' => true,
]]
]);
?>

to achieve your example you should use dropDownList instead of listBox
echo $form->field($model, 'additionalRoles', [
'options' => [
'class' => 'form-group',
],
])->dropDownList($additionalRoles, [
'class' => 'form-control j-multi-select2',
'prompt' => 'Select additional role',
]);

Related

Wrong rendering search field kartik select2

I use the select2 widget from kartik:
echo $form->field($model, 'person_ids')->widget(Select2::classname(), [
'data' => [1 => 'test1', 2 => 'test2'],
'theme' => Select2::THEME_KRAJEE_BS5,
'hideSearch' => true,
'options' => [
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
])->label('Personal');
The rendering of the search field is wrong - it intends the selected options. Any ideas how to fix this?

yii2 render remaining character in form field

I use jlorente remainingcharacters widget to show remaining character countdown for Inputfield in _form.php:
<?= $form->field($model, 'char52', ['showLabels'=>true])->widget(\jlorente\remainingcharacters\RemainingCharacters::classname(), [
'type' => \jlorente\remainingcharacters\RemainingCharacters::INPUT_TEXTAREA,
'text' => Yii::t('app', '{n} characters remaining'),
'label' => [
'tag' => 'p',
'id' => 'my-counter',
'class' => 'counter',
'invalidClass' => 'error'
],
'options' => [
'rows' => '1',
'class' => 'col-md-12',
'maxlength' => 52,
'placeholder' => Yii::t('app', 'Write something')
]
]) ?>
<?= $form->field($model, 'text', ['showLabels'=>true])->widget(\dosamigos\ckeditor\CKEditor::className(), [
'options' => ['rows' => 1],
'preset' => 'full'
]) ?>
As I rendered form fields the height of textInput is not the same as standard.
Is there any solution for this problem.
I am also searching for other widgets for remaining charachters as I want to use kartik\form\ActiveForm for rendering on the same text input remaining charachter count and ActiveField Prepend Addon.
Try adding "form-control" class to your widget:
'options' => [
...
'class' => 'col-md-12 form-control',
...
]

Yii2 Custom filter Dropdown for gridview

I am trying to add a custom filter named Sort By Month in the grid view header here is an example of the previous version of the site which I am revamping see below image
I was looking into the layout option of the grid view and added a drop-down in the layout template before {items}
GridView::widget(
[
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => '{summary}{errors}' . \kartik\widgets\Select2::widget(
[
'model' => $searchModel,
'attribute' => 'filter_month',
'theme' => \kartik\widgets\Select2::THEME_DEFAULT,
'data' => $searchModel->getFilterMonths(),
'pluginEvents' => [
"select2:select" => 'function() { $("#w2").submit();}',
// 'select2:select'=> new \yii\web\JsExpression("function(){console.log('here')}"),
],
'options' => [
'placeholder' => '--Select Month--',
],
'pluginOptions' => [
'allowClear' => true,
'width' => '160px',
],
]
) . '{items}{pager}',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'title',
'catalog',
'upc_code',
[
'attribute' => 'created_on',
'label' => 'Created On',
'filter' => \yii\jui\DatePicker::widget(['dateFormat' => 'yyyy-MM-dd', 'model' => $searchModel, 'attribute' => 'created_on']),
'format' => 'html',
],
[
'attribute' => 'status',
'label' => 'Status',
'format' => 'raw',
'value' => function ($data) {
switch ($data->status) {
case 0:
return "Being Edited (" . $data->created_on . ")";
break;
case 1:
return ($data->maxdate == '') ? 'Active' : 'Active';
break;
case 2:
return "Expired";
break;
}
},
],
['class' => 'yii\grid\ActionColumn'],
],
]
);
Now I want to submit the filters form when I select an option from the drop-down, but could not figure out how to attach the default filter submit event with the drop-down options so that it filters the results when I select any option in the drop-down.
For Custom filter you have used the perfect layout but you have to provide your custom field as a filterSelector.
"filterSelector" => "#". Html::getInputId($searchModel, 'AttributeName'),

Kartiv yii2 datepicker with model and custom id

I am using kartik datepicker extension http://demos.krajee.com/widget-details/datepicker in Yii2.
Issue :
If I use this with custom id for input it does not show show model validations
echo kartik\date\DatePicker::widget([
'model' => $objPatientModel,
'form'=>$objActiveForm,
'attribute' => 'date_of_birth',
'options' => ['placeholder' => 'Enter birth date ...', 'id' => 'patient_dob'], **// with id clientside validations for model does not work**
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'endDate' => date('d-m-Y'),
]
]);
While below code works
echo kartik\date\DatePicker::widget([
'model' => $objPatientModel,
'form'=>$objActiveForm,
'attribute' => 'date_of_birth',
'options' => ['placeholder' => 'Enter birth date ...'],**//id is not used here**
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'endDate' => date('d-m-Y'),
]
]);
But I want to use custom id .Any suggestions ?
You have to override the selectors, example:
$form->field($model, 'comment', ['selectors' => ['input' => '#myCustomId']])
->textarea(['id' => 'myCustomId']);?>
See https://github.com/yiisoft/yii2/issues/7627
<?=
$form->field($model, 'sales_date')->widget(DateControl::classname(), [
'name' => 'sales_date',
'value' => date('d-m-Y h:i:s'),
'type' => DateControl::FORMAT_DATETIME,
'autoWidget' => true,
'displayFormat' => 'php:d-m-Y h:i:s',
'saveFormat' => 'php:Y-m-d h:i:s',
'saveOptions' => [
'type' => 'hidden',
'form' => 'sales-form-red',
],
])
?>
by using the following option you can add extra form tag to input
'saveOptions' => [
'type' => 'hidden',
'form' => 'sales-form-red',
'class' => 'sales_date',
]
In my case out was
<input type="hidden" id="sales-sales_date" name="Sales[sales_date]" form="sales-form-red">
check by using link
Date Control Demo

Yii2: Menu active in different controllers

This is my code:
<?=
Nav::widget([
'options' => ['class' =>'nav-pills nav-stacked'],
'encodeLabels' => false,
'items' => [
[
'label' => 'Blog',
'url' => ['/blog'],
'active' => \Yii::$app->controller->id == 'blog'
],
],
]);
?>
But I want this menu is active not only when the controller is "blog", but also when it is "category" and "post".
Change your code like below:
Nav::widget([
'options' => ['class' =>'nav-pills nav-stacked'],
'encodeLabels' => false,
'items' => [
[
'label' => 'Blog',
'url' => ['/blog'],
'active' => in_array(\Yii::$app->controller->id,['blog','category','post'])
],
],
]);
By above code, your menu item will be considered as active if controller id was one of ['blog','category','post'] values. The only change was:
'active' => in_array(\Yii::$app->controller->id,['blog','category','post'])