【问题标题】:Yii2 validation on controller actionYii2 对控制器动作的验证
【发布时间】:2016-10-01 16:42:56
【问题描述】:

我正在开发一个 Yii 2.0 应用程序,用户可以在其中创建订单,然后将订单发送给审核,之后它会遵循工作流程中的多个阶段。

一切正常,直到昨天客户要求在发送订单之前审查订单是否被视为草稿。这意味着我必须在创建时关闭验证,并在用户单击“发送到审核”按钮时对其进行验证。我知道 Yii 2.0 支持场景,但可能场景并不适用于此,因为 Send To Review 按钮显示在只读视图中。这迫使我在控制器操作中进行验证,因为没有 send_to_review 视图。如何做到这一点(我的意思是控制器动作中的模型验证)?

这是控制器动作代码

public function actionSendToReview($id)
{
    if (Yii::$app->user->can('Salesperson'))
    {
        $model = $this->findModel($id);
        if ($model->orden_stage_id == 1 && $model->sales_person_id == Yii::$app->user->identity->id)
        {
            $model->orden_stage_id = 2;
            $model->date_modified = date('Y-m-d h:m:s');
            $model->modified_by = Yii::$app->user->identity->username;

            //TODO: Validation logic if is not valid show validation errors
            //for example "For sending to review this values are required:
            //list of attributes in bullets"
            //A preferred way would be to auto redirect to update action but 
            //showing the validation error and setting scenario to              
            //"send_to_review".


            $model->save();
            $this::insertStageHistory($model->order_id, 2);
            return $this->redirect(['index']);
        }
        else
        {
            throw new ForbiddenHttpException();
        }
    }
    else
    {
        throw new ForbiddenHttpException();
    }
}

我需要解决的是 TODO。 选项 1:在同一视图中显示验证错误,用户必须单击“更新”按钮更改请求的值保存,然后再次尝试发送到审核。 选项 2:自动重定向以更新在控制器中发现的已设置场景和验证错误的视图。

谢谢,

最好的问候

【问题讨论】:

    标签: validation yii2


    【解决方案1】:

    您可以在控制器中使用$model ->validate()进行验证。

    public function actionSendToReview($id)
    {
        if (Yii::$app->user->can('Salesperson'))
        {
            $model = $this->findModel($id);
            if ($model->orden_stage_id == 1 && $model->sales_person_id == Yii::$app->user->identity->id)
            {
                $model->orden_stage_id = 2;
                $model->date_modified = date('Y-m-d h:m:s');
                $model->modified_by = Yii::$app->user->identity->username;
    
    
    
                //TODO: Validation logic if is not valid show validation errors
                //for example "For sending to review this values are required:
                //list of attributes in bullets"
                //A preferred way would be to auto redirect to update action but 
                //showing the validation error and setting scenario to              
                //"send_to_review".
    
                //optional
                $model->scenario=//put here the scenario for validation;
    
                 //if everything is validated as per scenario
                 if($model ->validate())
                {                   
                   $model->save();
                   $this::insertStageHistory($model->order_id, 2);
                   return $this->redirect(['index']);
                }
                else
                {
                    return $this->render('update', [
                     'model' => $model,
                   ]);
                }
    
    
            }
            else
            {
                throw new ForbiddenHttpException();
            }
        }
        else
        {
            throw new ForbiddenHttpException();
        }
    }
    

    如果您不需要在actionCreate() 中进行验证。创建一个不验证任何字段的场景并在那里应用。

    【讨论】:

    • 它工作正常,但现在验证错误显示两次。奇怪
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2022-04-05
    • 2021-10-21
    • 2022-10-14
    相关资源
    最近更新 更多