【问题标题】:yii2 unique validation not workingyii2 唯一验证不起作用
【发布时间】:2016-02-18 16:43:13
【问题描述】:

Yii2 唯一验证不适用于员工 ID 和公司 ID 的组合 下面是我的模型代码。

            public function rules()
                {
                    return [
                        [['company_id', 'role_id'], 'required'],
                        [['company_id', 'role_id', 'status'], 'integer'],
                        [['employee_id'], 'string', 'max' => 15],
                        [['report_to'], 'string', 'max' => 16],
                        [['id',],'safe'],
                        ['employee_id', 'unique', 'targetAttribute' => ['company_id', 'employee_id'], 'message' => 'The combination of Company ID and Employee ID has already been taken.']
                        // ['employee_id', 'unique', 'targetAttribute' => ['company_id'], 'message' => 'The combination of Company ID and Employee ID has already been taken.']
                    ];
                }'

这是我的控制器代码

                $model = new Employee();
                //$profile = new Profile();
                // $profile->scenario = 'emp_bulk_uplscenariooad';
                if($model->load(Yii::$app->request->post())) {
                    $model->company_id = $userModel->company_id;

                    $model->employee_id = Yii::$app->request->post()['Employee']['employee_id'];

                    $model->role_id = Yii::$app->request->post()['Employee']['role_id'];
                    $model->report_to = Yii::$app->request->post()['Employee']['report_to'];
                        // print_r(!$model->validate());die();
                        if($model->save(false)){
        }

*************************************************************************

    please help me with this.thanks in advance

【问题讨论】:

    标签: validation yii2 unique


    【解决方案1】:

    你使用

      if($model->save(false)){ 
    

    这意味着不执行验证

    尝试使用

       if($model->save()){ 
    

    如果 model->save() 失败,你应该在 actionCreate 中调用 render create

    public function actionCreate()
    {
        $model =  new Employee();
    
        if($model->load(Yii::$app->request->post())) {
            $model->company_id = $userModel->company_id;
    
            $model->employee_id = Yii::$app->request->post()['Employee']['employee_id'];
    
                    $model->role_id = Yii::$app->request->post()['Employee']['role_id'];
                    $model->report_to = Yii::$app->request->post()['Employee']['report_to'];
            // print_r(!$model->validate());die();
             if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
             }
            else {
               return $this->render('create', [
                'model' => $model,
               ]);
           }
    }
    

    【讨论】:

    • 很抱歉有一个错误,,,我已经更新了答案..没有渲染创建无法工作..
    • 渲染后创建所有字段都是空的。我认为我不需要使用 jquery 进行此验证,因为 Yii2 唯一验证不起作用。希望有人能尽快解决这个问题。提前谢谢。
    【解决方案2】:

    尝试执行作曲家更新,修复了一个 Yii2 错误,该错误在使用关系数据以及其他场景时与唯一验证器相关联:

    请参阅以下内容:https://github.com/yiisoft/yii2/issues/14211

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      相关资源
      最近更新 更多