Forms Yii验证规则忽略';在';或';除';

Forms Yii验证规则忽略';在';或';除';,forms,validation,yii2,scenarios,Forms,Validation,Yii2,Scenarios,我面临着下一个问题。 我正在尝试以不同的方式验证字段,具体取决于操作:phone is在用户创建时应是唯一的,而不应在用户编辑时,因此: public function rules() { return [ [['owner_id','card_code', 'name', 'phone', 'birthday','language', 'last_ip'], 'required'], [['account_id'], 'required', 'o

我面临着下一个问题。 我正在尝试以不同的方式验证字段,具体取决于操作:phone is在用户创建时应是唯一的,而不应在用户编辑时,因此:

    public function rules()
{
    return [
        [['owner_id','card_code', 'name', 'phone', 'birthday','language', 'last_ip'], 'required'],
        [['account_id'], 'required', 'on' => 'update'],
        [['account_id'], 'exist', 'skipOnError' => true, 'targetClass' => Accounts::className(), 'targetAttribute' => ['account_id' => 'id'], 'on' => 'update'],

        [['owner_id', 'account_id', 'active', 'last_carwash'], 'integer'],
        [['birthday', 'last_active', 'last_update', 'last_sync'], 'safe'],
        [['car_number', 'role', 'status'], 'string'],
        [['card_code', 'car_number', 'name', 'phone', 'password', 'auth_key', 'language'], 'string', 'max' => 255],
        [['last_ip'], 'ip'],
        [['name'], 'string', 'min' => 5],

        ['password', 'string', 'min' => 6, 'message' => Yii::t('app', 'The password must contain at least 6 symbols.')/*, 'on' => 'create'*/],
        ['password', 'match', 'pattern' => '/[A-z]/', 'message' => Yii::t('app', 'The password must contain at least 1 letter.')/*, 'on' => 'create'*/],
        ['password', 'match', 'pattern' => '/[0-9]/', 'message' => Yii::t('app', 'The password must contain at least 1 number.')/*, 'on' => 'create'*/],
        [['password'], 'required', 'on' => 'create'],
        [['phone'], 'validatePhone', 'except' => 'update'/*, 'skipOnEmpty' => false*/],
        [['email'], 'email'],
        [['email'], 'validateEmail', 'except' => 'update'/*, 'skipOnEmpty' => false*/],
    ];
}
我正在使用AJAX验证:

$form = ActiveForm::begin([
        'enableAjaxValidation' => true,
        'validationUrl' => \yii\helpers\Url::to('/user/validation'),
    ]);
验证操作:

public function validatePhone()
  
  {
        $phone = preg_filter( '/[^0-9]*/','', $this->phone );
        $u = Users::findOne(['phone' => $phone]);
        if($u)
            /*if($this->id != $u->id)*/
                $this->addError('phone', Yii::t('app', 'This phone is already taken.'));
    }
    public function validateEmail()
    {
        $email = $this->email;
        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
            $this->addError('email', Yii::t('app', 'Please, enter correct email'));
    
        $u = Users::findOne(['email' => $email]);
        if($u)
            $this->addError('email', Yii::t('app', 'This email is already taken.'));
    }
在控制器动作中

   public function actionCreate()
        {
            if ( Yii::$app->request->post() != NULL ) {
            $data = Yii::$app->request->post('Users');
            $model = new Users;
            $model->scenario = 'create';
           ...//lots of a code
            if(!$model->validate()){var_dump($model->errors);exit;}
            $model->save();
             return $this->redirect(['view', 'id' => $model->id]);
        }

    public function actionUpdate($id)
        {
          $model = $this->findModel($id);
          $model->scenario = 'update';
          $model->setScenario('update');// same as ^ , but who knows...
         $data = Yii::$app->request->post();

         if( $data )
         {
        ...//lots of code
           if(!$model->validate()){var_dump($model->errors);exit;}
           $model->save();
             return $this->redirect(['view', 'id' => $model->id]);
          }
         return $this->render('update', [
            'model' => $model,
          ]);
       }

public function actionValidation()
    {
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

            $model = new \app\models\Users();
            if($model->load(Yii::$app->request->post()))
                return \yii\widgets\ActiveForm::validate($model);
        }
        throw new \yii\web\BadRequestHttpException( Yii::t('app', 'Bad request.') );
    }

    protected function findModel($id)
        {
            if (($model = Users::findOne($id)) !== null) {
                return $model;
            }
            throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
        }
情景:

public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['create'] = ['account_id', 'card_code', 'name', 'phone', 'email', 'car_number', 'birthday', 'role', 'password'];
        $scenarios['update'] = ['account_id', 'card_code', 'name', 'phone', 'email', 'car_number', 'birthday', 'role', 'password'];
        return $scenarios;
    }
但它在任何形式下都不起作用

如果我删除“=>”创建“它可以正常工作,但在所有形式中,都是不需要的。
那么,我如何仅在创建场景中使用validatePhone和validateEmail呢?

on的功能在框架套件中经过了良好的测试,因此它一定是您的一部分。请用actionCreate和actionUpdate的其余代码更新您的问题。@Bizley,添加了更多代码。谢谢你的关注。有很多多余的代码,但我没有看到任何真正的错误。请记住,如果内联验证器的属性为空或已经存在一些错误(您需要将
skipOnError
skipOnEmpty
设置为
false
),则内联验证器(如
validatePhone
skipOnEmpty
将无法工作。不管怎样,在什么情况下验证不起作用?在客户端触发的AJAX验证还是在服务器验证?如果是前者-添加
actionValidation
的代码。添加actionValidation控制器代码。验证工作正常,但我希望它只在一个场景中工作(创建)。但目前它在任何地方都能工作,例如创建和更新。在“=>”创建“或“”上添加
”除了“=>”更新“
”之外,在任何地方添加
”都没有效果或中断验证,但看看您的actionValidation代码-那里没有设置场景,这不是您声称它不起作用的原因吗?