st.limp
st.limp

Reputation: 141

Yii2 Dropdown list value in controller

I have a dropdown list in my view and would like to send an email later from controller

create view:

echo $form->field($model, 'company')->dropDownList(['a' => 'LG', 'b' => 'Samsung'], ['prompt'=>''])->label('Company');

action code:

public function actionVacancy()
{
    $model=new VacancyForm;

    if($model->load(Yii::$app->request->post()) &&$model->validate())
    {

      Yii::$app->mailer->compose('vacancy',[
                        ...
                        'company'=> $model->company,
                        ...
      ])

What is the proper way to access dropdown list selected value? Currently I can only access key, but don't understand how to access value

Thanks in advance

Upvotes: 2

Views: 1984

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

In the controller you obtain the result of the POST. In this case you get the value of the id related with your dropdown because this is value send by post.

If you want obtain the description in controller yon need find the descriptio related to the model by id.

 use yourapp\models\Company;

 $company = Company::find()
->where(['id' => $model->company])
->one();

assuming $ model->company contains the id of the istance you are looking for

  $company->name  //  

should contain the desired value.

Upvotes: 1

Coz
Coz

Reputation: 1983

In your controller you need to load the POST values into the model before you can access them.

$model = new Model();

if ($model->load(Yii::$app->request->post())) {

//send email
$company = $model->company

...

Upvotes: 3

Related Questions