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' => ...
Related
I'm using CakePHP Form helper to create form input fields. When a user edit an item, it will pre-fill the fields with data. I use v-model on each input field to make sure data is correctly populated.
It's all good for the textboxes as data is bound correctly. But for <select> fields, v-model doesn't seem to work. I've tried using default property for the Form->input() but no help. Here's some code:
<div class="fields">
<?= $this->Form->input('email', [
'label' => __('Email'),
'type' => 'email',
'v-model' => 'emailModel',
'placeholder' => __('Required'),
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
<?= $this->Form->input('status', [
'label' => __('Status'),
'type' => 'select',
'default' => '{{userStatus}}',
'options' => [
1 => 'Active',
2 => 'Inactive'
],
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
</div>
The email textbox has correct data filled, but not for the status selectbox. But if I replace {{userStatus}} with, for example, 2, it will set the default option to Inactive, even though {{userStatus}} itself has value 2.
How do I go about solving this?
I am new to VueJS but I came across the same question. The VueJS documentation states:
v-model will ignore the initial value, checked or selected attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data option of your component.
https://v2.vuejs.org/v2/guide/forms.html#Basic-Usage
Therefore, you need to assign your data.userStatus with your default value.
var vm = new Vue({
el: '#app',
data: {
statusModel: 2 // The default value goes here
}
});
PHP Side:
<div class="fields">
<?= $this->Form->input('email', [
'label' => __('Email'),
'type' => 'email',
'v-model' => 'emailModel',
'placeholder' => __('Required'),
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
<?= $this->Form->input('status', [
'label' => __('Status'),
'type' => 'select',
'v-model' => 'statusModel',
'options' => [
1 => 'Active',
2 => 'Inactive'
],
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
</div>
Hope this helps.
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(''); ?>
I am new to this framework, and in the english language too (haha).
But here is my problem:
I do some code using the belongsto of the cake. I think I did everything right. But when I click submit, he only register the information of the table and dont do the association.
My tables:
Planos
Servidores
Servidores_Planos
My code:
table/PlanosTable.php
$this->belongsToMany('Servidores', [
'class' => 'servidores',
'joinTable' => 'servidores_planos',
'foreignKey' => 'planos_id',
'targetForeignKey' => 'servidores_id'
]);
table/ServidoresTable.php
$this->belongsToMany('Planos', [
'joinTable' => 'servidores_planos',
'foreignKey' => 'servidores_id',
'targetForeignKey' => 'planos_id'
]);
I dont think it is necessary to show the controller code, because the add page from planos receive the servidores.
And this is my input in add page.
<?php echo $this->Form->input('servidores.id',array('type'=>'select','multiple','label'=>false,'id'=>'selectServidor', 'class'=>'form-group', 'name' => 'servidores[]', 'options'=> $servidores,'style' => 'width:430px;height:180px;')); ?>
You just need to modify your view slightly. Try this :
echo $this->Form->input('servidores._ids', [
'type' => 'select',
'multiple' => true,
'label' => false,
'id' => 'selectServidor',
'class' => 'form-group',
//'name' => 'servidores[]', /* This name field is generated automatically */
'options' => $servidores,
'style' => 'width:430px;height:180px;'
]);
This should solve your issue. In your controller, make sure you're following the convention and passing the appropriate parameters for saving associated data.
Hope this helps.
Peace! xD
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('');
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!