Kartik DepDrop at update form not working in Yii2 - yii2

For example, we have an update form with the fields
<?= $form->field($model, 'company_name')->dropDownList($data,
['prompt' => 'Select Company Name..', 'id' => 'cat-id']
) ?>
<?php
echo $form->field($model, 'employee_name')->widget(DepDrop::classname(), [
'options'=>['id'=>'subcat-id'],
'pluginOptions'=>[
'initialize' => true,
'depends'=>['cat-id'],
'placeholder'=>'Select...',
'url'=>yii\helpers\Url::to(['claim/subcat'])
]
]);
?>
A value of depended dropdown is not set when the first dropdown has a value ($cat-id). It shows placeholder "Choose a model" instead of list of models for current manufacturer. The same happens, when depended dropdown also have a value ($model->model_id). It is not showed. Only placeholder "Choose a model" is showed

You must do one of the following for update:
Option 1: Set data property array for dependent dropdown to have a preset dependent list on init.
Option 2: Set pluginOptions['initialize'] property to true for dependent dropdown. This will run the ajax calls on init to generate the dropdown list.

Related

Yii2 select2.How to select multiple items without closing the selection dropdown

I am using yii2 select2 in my project.And the code is
<?= Select2::widget([
'name' => 'drp-amenties',
'data' => Amenties::getAmentiesEnglish(),
'options' => [
'id'=>'drp-amenties',
'placeholder' => 'Select Amenties',
'multiple' => true
]
]); ?>
And the output is like this .
The code is working. When I click the select2 it opens the selection dropdown as shown in the image. I can select multiple items. But the problem is I can only select items one by one. Means when I select one item, the select dropdown is closing. And then I want to click the select2 to open the dropdown for selecting the section item. I want it like selecting multiple items for the first time opening.

yii2 autocompelete not work for me

i have table that have developer_id, name, family .etc columns
i want to show suggest name in input in view i did something like this but this give me a input whitout any suggestion and autocompelte
why?
$data = Developers::find()
->select(['name as value', 'name as label','developer_id as id'])
->asArray()
->all();
echo AutoComplete::widget([
'name' => 'dname',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
'select' => new JsExpression("function( event, ui ) {
$('#aa').val(ui.item.id);
}")],
]);
?>
<input id="aa" value="" type="hidden">
I copied and pasted your code in one of my views. I just changed your model so I am using one of my models (tables). Your code works perfectly in my view. So I think you should check if the problem is one of these:
You are not correctly importing one of these:
use backend\models\Developers;
use yii\jui\AutoComplete;
use yii\web\JsExpression;
Your table Developers is empty
In the part of your code that says:
->select(['name as value', 'name as label','developer_id as id'])
Are you sure your table developer has the columns name and developer_id?

How to add id preix for field in activeForm?

i have two forms holds the same model attributes, since Yii2 generate the field id to be ModelName-fieldName so the field generated will be as follow:
<select name="Channel[channel]" class="form-control" id="channel-description">
i have tried to use fieldConfig in Activeform but it doesn't add the id to the field itself.
You should simply use the third parameter of ActiveForm::field() :
$options : The additional configurations for the field object.
e.g. :
$form->field($model, 'channel', ['inputOptions' => ['id' => 'channel-description']])
Read more about ActiveForm::field().
But if you really want to add a prefix to all your fields ids, you should override ActiveForm.
If you want save input id structure "{model}-{attribute}".
Use yii\helpers\Html::getInputId() for generate "{model}-{attribute}" input id and complete it with your custom prefix.
$form->field($model, 'name')->textInput(['id' => 'custom-' . Html::getInputId($model, 'name')])
If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.
<?= $form->field($searchModel, 'regId',[
'selectors' => ['input' => '#company-vacancy-regId'],
'inputOptions' => ['id' => 'company-vacancy-regId'],
])->widget()?>

How to get value of dropdown instead of id yii2

i am using a dropdown list and i wants to know is there any way i can get the value selected by user instead of id.
I am using yii2 activeform with ArrayHelper map to populate the options which is like below
<?= $form->field($model, 'carname')->dropDownList(
ArrayHelper::map(Cars::find()->all(), 'id', 'name'),
['prompt' => 'Select Car Name']
) ?>
In controller on form submit it returns id which is good but i want to save the name directly in the database.
Thank you
<?= $form->field($model, 'carname')->dropDownList(
ArrayHelper::map(Cars::find()->all(), 'name', 'name'),
['prompt' => 'Select Car Name']
) ?>

How to set cakephp radio-button from mysql?

This cakephp form used to edit a mysql record needs to load the state of a radio button from a mysql database.
The mysql payment_type is enum('Account', 'Credit').
All of the other non-radio-button form inputs reload from the database and payment_type is correctly displayed on another form using this:
<?php echo h($purchaseOrder['PurchaseOrder']['payment_type']); ?>
Why doesn't this correctly set the radio-button from payment_type?
$options = array('account' => 'Account', 'credit' => 'Credit');
$attributes = array('legend' => false, 'value' => 'payment_type');
echo $this->Form->radio('payment_type', $options, $attributes);
In your attribute array, you should assign value which you want to keep selected by default .
For example you want account to be selected by default then in value you should assign 'account'. So your final attribute will be:
$attributes = array('legend' => false, 'value' => 'account');