Yii2:如何在控制器中使用单选按钮值进一步执行?

Yii2: How to use radio button value in controller for further execution?

我需要从单选列表中选择一个值。如果选择了特定值,则只有下一次执行才会发生,否则只会发生更新。

电台列表-

<?= $form->field($model, 'status')->radioList(array('Approved'=>'Approved','Digital'=>'Digital','CDP'=>'CDP','Print'=>'Print','Other Process'=>'Other Process','Packing'=>'Packing','Dispatch'=>'Dispatch'),['class' => $model->status ? 'btn-group' : 'btn btn-default'],['id'=>'radioButtons'] );   

控制器-

public function actionCreate()
    {
        $model = new Status();


        if ($model->load(Yii::$app->request->post())) 
        {
               if(radio button value is approved or dispatch below code should execute )
              {
                Yii::$app->mailer->compose()
                ->setFrom('abc@gmail.com')
                ->setTo('qwer@hotmail.com')
                ->setSubject('Message subject')
                ->setTextBody('Plain text content')
                ->setHtmlBody('<b>HTML content</b>')
                ->send();
                }
                  else{ 
                         (just update the form )
                    }

                $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } 
        else 
        {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

我知道它很简单,但没有找到实现它的方法

您需要检查此值的 $model->status 属性。

if($model->status == 'Approved' || $model->status == 'Dispatch')
{
    //do stuff
}

为了方便起见,我建议您将值存储为常量。例如

class MyActiveRecord extends \yii\db\ActiveRecord
{
    const STATUS_APPROVED = 1;
    const STATUS_DISPATCH = 2;
    //and so on

    public static function getStatusList()
    {
         return [
             self::STATUS_APPROVED => 'Approved',
             self::STATUS_DISPATCH => 'Dispatch',
             //other values
         ];
    }
}

然后在控制器中

if($model->status == MyActiveRecord::STATUS_APPROVED ||
   $model->status == MyActiveRecord::STATUS_DISPATCH)
{
    //do stuff
}

此外,它还会使您的视图更具可读性:

<?= $form->field($model, 'status')->radioList($model->getStatusList())?>