I have two api's:
1: returns all the industries,
2: returns all the industry category(based on industry id).
I need two dropdowns, one dependent on other. On selecting industry 2nd dropdown should show only relevant categories.
Thanks in advance.
I got it. I have simply used ajax which posts the value from one dropdown and sends the data to an action which returns the data and i am simply putting those values to my other drop down. :)
<?= $form->field($model, 'industryId')->dropDownList($industry,
['prompt'=>'Select Industry',
'onchange'=>'
$.get( "'.Url::toRoute('/site/category').'", { id: $(this).val() } )
.done(function( data ) {
$( "#'.Html::getInputId($model, 'industryName').'" ).html( data );
}
);
','class' => 'form-control'
]
); ?>
<?= $form->field($model, 'industryName')
->dropDownList(
['prompt'=>'Select category','class' => 'form-control']);
?>
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Category;
?>
<?php $form = ActiveForm::begin(); ?>
$model = Category::find()->select('id,name')->orderBy('name asc')->all();
$listData = ArrayHelper::map($model, 'id', 'name');
<?= $form->field($model, 'industryId')->dropDownList($listData,
['prompt'=>'Select Category',
'onchange'=>'
$.get( "'.Url::toRoute('/category/subcats').'", { id: $(this).val() } )
.done(function( data ) {
$( "#'.Html::getInputId($model, 'sub_category').'" ).html( data );
}
);
','class' => 'form-control'
]
); ?>
<?= $form->field($model, 'sub_category')
->dropDownList(
['prompt'=>'Select sub cat','class' => 'form-control']);
?>
----
You can use this extension. You can find explanation of plugin on its guide page.
Related
I am trying to display a link in error message in login for, but it is not working.
The error message in LoginForm valdiation:
$this->addError($attribute, 'Your account has been disabled. Enable It');
In login.php (view):
<?= $form->errorSummary($model); ?>
I tried like below, but not working:
<?= $form->errorSummary($model,['errorOptions' => ['encode' => false,'class' => 'help-block']]); ?>
I am getting the following output instead of rendered a tag:
You need to disable encoding at ActiveForm level using encodeErrorSummary property, if you want to use $form->errorSummary($model):
<?= $form = ActiveForm::begin([
'id' => 'login-form',
'encodeErrorSummary' => false,
'errorSummaryCssClass' => 'help-block',
]) ?>
<?= $form->errorSummary($model) ?>
Alternatively you may use Html::errorSummary() directly:
<?= Html::errorSummary($model, ['encode' => false]) ?>
<?= $form->field($model, 'fk_int_category_id')->dropDownList(
ArrayHelper::map(TblCategory::find()->all(), 'pk_category_id','vchr_category_name'),
['prompt'=> 'Select a category',
'onchange'=>'
$.post("index.php?r=product-size-variants/get-sub-category&id='.'"+$(this).val(), function(data){
//alert(data);
$("select#tblproduct-fk_int_sub_category_id").html(data);
});',
]) ?>
<?= $form->field($model, 'fk_int_sub_category_id')->dropDownList(
ArrayHelper::map(TblSubCategory::find()->all(), 'pk_sub_category_id','vchr_sub_category_name'),
['prompt'=> 'Select Sub category'
'onchange'=>'
$.post("index.php?r=product-size-variants/get-sub-category&subId='.'"+$(this).val(), function(data){
//alert(data);
$("select#tblproduct-fk_int_sub_category_id").html(data);
});'
]) ?>
<?= $form->field($model, 'fk_int_product_variants')->dropDownList(
ArrayHelper::map(TblProductSizeVariants::find()->all(), 'pk_int_product_size_variants_id','vchr_size_names'),
['prompt'=> 'Select Product Size']) ?>
i have three dropdownlist In second dropdownlist work using first one,
and third dropdownlist dependent to 1st and 2nd dropDown.
how to i pass 2 values in this index.php?r=product-size-variants/get-sub-category&subId= url??? of second dropdown
Your code is kind of mess, you might get caught error in writing js in php, where excape sequence has to be exectly to be same. i have done re-work. let me know if it working
<?= $form->field($model, 'fk_int_category_id')->dropDownList(
ArrayHelper::map(TblCategory::find()->all(), 'pk_category_id','vchr_category_name'),
['prompt'=> 'Select a category',
'onchange'=>'generateSubCat(this)',
]) ?>
<?= $form->field($model, 'fk_int_sub_category_id')->dropDownList(
ArrayHelper::map(TblSubCategory::find()->all(), 'pk_sub_category_id','vchr_sub_category_name'),
['prompt'=> 'Select Sub category'
'onchange'=>'generateProductVariant(this)'
]) ?>
<?= $form->field($model, 'fk_int_product_variants')->dropDownList(
ArrayHelper::map(TblProductSizeVariants::find()->all(), 'pk_int_product_size_variants_id','vchr_size_names'),
['prompt'=> 'Select Product Size']) ?>
<script>
function generateSubCat(obj){
$.post("index.php?r=product-size-variants/get-sub-category/?id="+obj.value, function(data){
$("select#tblproduct-fk_int_sub_category_id").html(data);
});
}
function generateProductVariant(obj){
var catID=$('#tblproduct-fk_int_category_id').val();
if(catID==""){
alert("Please select category");
}else{
$.post("index.php?r=product-size-variants/get-sub-category/?id="+catID+"&subId="+obj.value, function(data){
$("select#tblproduct-fk_int_sub_category_id").html(data);
});
}
}
</script>
Please refer to following url for dependent dropdown that can help you.
http://demos.krajee.com/widget-details/depdrop#basic-usage
In my form I update more start and end dates from the same model at once. See the simplified form:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $i=>$date): ?>
<?= $form->field($date,"[$i]start"); ?>
<?= $form->field($date,"[$i]end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
In the model I need to control, if the end date is after the start date:
public function rules() {
return [
[['end'], 'compare', 'compareAttribute' => 'start', 'operator' => '>', 'message' => '{attribute} have to be after {compareValue}.'],
];
}
I tried to change selectors similarly as described in: Yii2: Validation in form with two instances of same model, but I was not successful. I suppose I need to change the 'compareAttribute' from 'mymodel-start' to 'mymodel-0-start' in the validation JS:
{yii.validation.compare(value, messages, {"operator":">","type":"string","compareAttribute":"mymodel-start","skipOnEmpty":1,"message":"End have to be after start."});}
So, I look for something like:
$form->field($date,"[$i]end", [
'selectors' => [
'compareAttribute' => 'mymodel-'.$i.'-start'
]
])
Solution
The solution is based on the answer of lucas.
In the model I override the formName() method, so for every date I have a unique form name (based on ID for the existing dates and based on random number for new dates):
use ReflectionClass;
...
public $randomNumber;
public function formName()
{
$this->randomNumber = $this->randomNumber ? $this->randomNumber : rand();
$number = $this->id ? $this->id : '0' . $this->randomNumber;
$reflector = new ReflectionClass($this);
return $reflector->getShortName() . '-' . $number;
}
The form then looks like this:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $date): ?>
<?= $form->field($date,"start"); ?>
<?= $form->field($date,"end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
Override the formName() method in your model class to make it unique. If you don't want to change your model class, create a subclass of it for the purpose of working for this controller action. After doing this, the html ID and name fields will automatically be unique.
I am pretty sure that there is a better way to populate the array, needed for the dropdown:
<?php
$items2 = [Yii::$app->user->identity->id => Yii::$app->user->identity->username ]; ?>
<!--...some html -->
<?= $form->field($model, 'idUser')->dropDownList($items2,['Item' => ''])?>
already try:
$item2 = ArrayHelper::map(Yii::$app->user->identity::find()->all(), 'id', 'name');
reason, I want to display 'name' but submit 'value'='id'.
Should be this
<?= $form->field($model, 'idUser')->
dropDownList(ArrayHelper::map(Yii::$app->user->identity->find()->all(),
'id', 'username'), ['prompt'=>'Select...'])?>
I am using yii 2.0 Framework.
How i can make options from my database.
I found this, but it's yii 1.1:
<?php echo CHtml::dropDownList('listname', $select,
array('M' => 'Male', 'F' => 'Female'));
I want to pass it to form:
<?php $form->dropDownList() ?>
How i can fill my dropdownlist from my database table?
If you use ActiveForm widget use this:
<?php
$items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
$form->field($model, 'attribute')->dropDownList($items)
?>
Use yii\helpers\Html it contains Html::dropDownList().
echo Html::dropDownList('listname', $select, ['M'=>'Male', 'F'=>'Female']);
Check Yii Framework 2.0 API
Controller
public function actionSomething() {
$sexes = ['M'=>'Male', 'F'=>'Female'];
$this->render('yourView', ['sexes'=>$sexes]);
}
View
<?php
::
echo Html::dropDownList('listname', $select, $sexes);
::
?>
Yes if you use ActiveForm widget , u dont have to change anything in the controller , in views, in the form, add this where u want the dropdown
use yii\helpers\ArrayHelper;
<?php
$city = \app\models\City::find()->all();
$listData=ArrayHelper::map($city,'cityId','cityName');
?>
<?= $form->field($model, 'cityId')->dropDownList($listData,['prompt'=>'Choose...']) ?>
<?= $form->field($model, 'name_of_field')->dropdownList(['1' => 'aaa', '2' => 'bbb'], ['prompt' => '---Select Data---']) ?>