mcki0127
(Steve)
October 29, 2015, 4:00am
1
I am new to Yii and am having trouble wrapping my head around how the forms work. I am using a dropDownList like this:
<?= $form->field($model, 'type')->dropDownList(Type::find()->select(['type'])->indexBy('id')->column(), ['prompt'=>'Select Profile Type'])->label('Profile Type') ?>
<div>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
</div>
My problem is that I want the form to post the ‘type’ attribute, but it only posts the ID. If I remove ->indexBy(‘id’), it always posts “0”. If I remove ->column() then the dropdown is populated with giberish. How can I get get the form to post the selected value? What if I wanted to return another column than the one selected in the dropdown? Is it possible to do that?
I’m not sure if this is a related question, but what are the variables in the models for? Are they supposed to capture the posted values? I can’t get them to work and they seem to be useless to me. If I leave them declared, they seem to mess up any posted values. Rather than using $type, I can just use $this->type. What are they for?
Thanks for any help!
I am new to Yii and am having trouble wrapping my head around how the forms work. I am using a dropDownList like this:
<?= $form->field($model, 'type')->dropDownList(Type::find()->select(['type'])->indexBy('id')->column(), ['prompt'=>'Select Profile Type'])->label('Profile Type') ?>
<div>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
</div>
Thanks for any help!
This
link will helps You.
$items = ArrayHelper::map(Category::find()->asArray()->all(), 'id', 'name');
//or
//Category::find()->select(['last_name'])->limit(5)->indexBy('id')->column());
echo $form->field($model, 'name_field')->dropDownList(
$items],
['value' => selected value, 'class' => '']
);
haidt
(Haidt3004)
October 29, 2015, 4:28am
3
I am new to Yii and am having trouble wrapping my head around how the forms work. I am using a dropDownList like this:
<?= $form->field($model, 'type')->dropDownList(Type::find()->select(['type'])->indexBy('id')->column(), ['prompt'=>'Select Profile Type'])->label('Profile Type') ?>
<div>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
</div>
My problem is that I want the form to post the ‘type’ attribute, but it only posts the ID. If I remove ->indexBy(‘id’), it always posts “0”. If I remove ->column() then the dropdown is populated with giberish. How can I get get the form to post the selected value? What if I wanted to return another column than the one selected in the dropdown? Is it possible to do that?
I’m not sure if this is a related question, but what are the variables in the models for? Are they supposed to capture the posted values? I can’t get them to work and they seem to be useless to me. If I leave them declared, they seem to mess up any posted values. Rather than using $type, I can just use $this->type. What are they for?
Thanks for any help!
Hello ,
If you want to post value of type field you should update like this:
Type::find()->select(['type'])->indexBy('type')->column()
The value return will :
array(
'value_of_type" => "value_of_type"
)
To get other value:
Type::find()->select(['id,type,other_field...'])->indexBy('type')->column()
The value you want to post will be here : [size=2]indexBy(‘type’)[/size]
mcki0127
(Steve)
October 29, 2015, 2:22pm
4
Thank you. That did the trick. I thought I had tried indexBy(‘type’), but then again I have been trying a hundred different things, so I’m not sure.