I am trying to replace yii default select widget with yiiwheels select widget.
Code using yii select widget
<?php echo $form->dropDownList($model,'branch_id', CHtml::listData(Branches::model()->findAll(array('order' => 'branch_name')),'id','branch_name'));?>
Tring to get something like this
<?php $this->widget('yiiwheels.widgets.formhelpers.WhSelectBox',array('name' => 'branch_id', 'size' => 'input-large', 'model' => $model, 'data' => CHtml::listData(Branch::model()->findAll(array('order' => 'branch_name')),'id','branch_name')));?>
I get an error on form submission that branch_id field can't be blank.
How do i associate it with the current form model?
Here is a link to Yiiwheels API docs
Instead of 'name' field, i had to use 'attribute' field.
<?php $this->widget('yiiwheels.widgets.formhelpers.WhSelectBox',array('attribute' => 'branch_name', 'size' => 'input-large', 'model' => $model, 'data' => CHtml::listData(Branch::model()->findAll(array('order' => 'branch_name')),'id','branch_name')));?>
Related
Currently, I have an existing code to show user status as 'Inactive' or 'Active' based on ban_time field of table User. When status of User is 'Inactive', the ban_time field will be updated by current timespan (I guess it use external wrapper plugin)
$form->field($user, 'ban_time')->widget(SwitchInput::classname(), [
'type' => SwitchInput::CHECKBOX,
'containerOptions' => ['class' => 'inner-form-group'],
'pluginOptions' => [
'state' => empty($user->ban_time),
'handleWidth' => 60,
'onText' => 'Active',
'offText' => 'Inactive'
],
'pluginEvents' => [
"switchChange.bootstrapSwitch" => "function(event, state) { $('[name=\'User[ban_time]\']').val(state ? 0 : 1) }",
]
])->label('Status');
Now, I need to add more status instead of 'Inactive' or 'Active'. So I want to change this field into dropDownList but when changing status of User, ban_time was not changed
$form->field($user, 'ban_time')->dropDownList(
[empty($user->ban_time) =>'Active', !empty($user->ban_time) =>'Inactive']
)->label('Status');
Please help me how to change it
for example if you have dropdownlist like this given below
echo $form->dropDownListGroup(
$model, 'status', array(
'wrapperHtmlOptions' => array(),
'widgetOptions' => array(
'data' =>$model->getDropdownvalue(),
'htmlOptions' => array(
'prompt' => 'Select Project',
'ajax' => array(
'type' => 'POST',
'url' => your url,
//'dataType' => 'json',
'data'=>array('status'=>'js:this.value'),
)
in your controller you will get the value of dropdown list using url
public function actiondropdownvalue(){
$model = new status();
$status = $_POST['status'];
$model->save();
this example is only showing how it will work . You will need user id to save status for particular user to update or save status.
You can make your form element as dropDownList as
$items = [1 =>'Active', 0 =>'Inactive' ,2 => 'Subscribed' ,3 => 'Deleted'];
$form->field($user, 'ban_time')->dropDownList($items)->label('Status');
See DropDownList
Problem :
I was trying to build auto suggestion select box in my yii2 application.I have downloaded from here https://github.com/kartik-v/yii2-widget-select2 and used select2 widget . below is my code
use kartik\select2\Select2;
use app\models\Contact;
use yii\helpers\ArrayHelper;
$data = ArrayHelper::map(Contact::find()->where(['user_id'=>Yii::$app->user->getId()])->orderBy('email')->asArray()->all(), 'id', 'email');
echo $form->field($model, 'contact_id')->widget(Select2::classname(), [
'data' => $data,
'options' => ['placeholder' => 'Search a contact or add new','multiple' => true],
'pluginOptions' => [
'allowClear' => true,
'tags' => true,
'maximumInputLength' => 1000
],
]);
Here contact is model and give array data of email records like test1#gmail.com,test2#gmail.com
but when user search string in dropdownbox it produce output as attachment image
I tried
$this->enableCsrfValidation = false;
in my controller with respective action but no luck,
is there any other solution , i have google an hour but there no solution found
I am using the yii2 auto complete widget, it is working fine, except when I update the form the field shows blank.
use yii\jui\AutoComplete;
use yii\web\JsExpression;
$data = app\models\Doctor::find()
->select(['doctor_name as value', 'doctor_name as label','id as id'])
->asArray()
->all();
and after that the auto-complete code like this
echo 'Doctor' .'<br>';
echo AutoComplete::widget([
'name' => 'Doctor',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'select' => new JsExpression("function( event, ui ) {
$('#appoinment-doctor_name').val(ui.item.id);
}")],
]);
<?= Html::activeHiddenInput($model, 'doctor_name')?>
What I am missing here? How can I get the value on update?
You have to explicitly set value:
echo AutoComplete::widget([
'name' => 'Doctor',
'id' => 'ddd',
'value' => $model->doctor_name,
...
This isn't necessary if you set model and attribute though.
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' => ...
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!