Php 防止对更新进行验证

Php 防止对更新进行验证,php,yii,yii2,Php,Yii,Yii2,更新记录时如何防止验证?我需要防止它验证电子邮件、密码和密码确认。这是目前的规则: public function rules() { return [ [['first_name', 'last_name', 'email', 'password', 'password_confirm'], 'required'], [['role_id', 'state_id', 'country_id', 'zip'], 'integ

更新记录时如何防止验证?我需要防止它验证电子邮件、密码和密码确认。这是目前的规则:

public function rules()
    {
        return [
            [['first_name', 'last_name', 'email', 'password', 'password_confirm'], 'required'],
            [['role_id', 'state_id', 'country_id', 'zip'], 'integer'],
            [['first_name', 'last_name', 'email', 'password', 'address', 'city'], 'string', 'max' => 255],
            [['password', 'password_confirm'], 'string', 'min' => 8],

            ['password_confirm', 'compare', 'compareAttribute' => 'password'],
            ['email', function($attribute) {
                $user = static::findOne(['email' => Yii::$app->encrypter->encrypt($this->{$attribute})]);
                if ($user) {
                    $this->addError($attribute, 'This email is already in use.');
                }
            }],
            ['email', 'email'],

        ];
    }

尝试使用核心事件:beforeValidate。您可以了解它

尝试使用核心事件:beforeValidate。您可以了解它

尝试使用核心事件:beforeValidate。您可以了解它

尝试使用核心事件:beforeValidate。您可以阅读有关它的内容

以防止验证您可以定义的特定操作上的特定属性,但参数除外。 例如:


然后在实例化新模型时的更新操作中,只需将scenario设置为update
$model->scenario='update'

,以防止验证可以定义的特定操作上的特定属性,但参数除外。 例如:


然后在实例化新模型时的更新操作中,只需将scenario设置为update
$model->scenario='update'

,以防止验证可以定义的特定操作上的特定属性,但参数除外。 例如:


然后在实例化新模型时的更新操作中,只需将scenario设置为update
$model->scenario='update'

,以防止验证可以定义的特定操作上的特定属性,但参数除外。 例如:


然后在实例化新模型时的更新操作中,只需将scenario设置为update
$model->scenario='update'
Sasha为什么要阻止更新验证?但是,这是一种不好的做法,如果您希望按照自己的方式使用代码:

首先,您需要在模型中创建一个场景

namespace app\models;

use yii\db\ActiveRecord;

class {YourClass} extends ActiveRecord
{
  /** Other stuff above */

public function scenarios(){

  $scenarios = parent::scenarios(); 
  $scenarios['update'] = ['!email', '!password', '!password_confirm'];

 return $scenarios;
 }


public function rules()
    {
        return [
            [['first_name', 'last_name'], 'required'],
            [['email', 'password', 'password_confirm'], 'safe', 'on'=>'update'],
            [['role_id', 'state_id', 'country_id', 'zip'], 'integer'],
            [['first_name', 'last_name', 'email', 'password', 'address', 'city'], 'string', 'max' => 255],
            [['password', 'password_confirm'], 'string', 'min' => 8],

            ['password_confirm', 'compare', 'compareAttribute' => 'password'],
            ['email', function($attribute) {
                $user = static::findOne(['email' => Yii::$app->encrypter->encrypt($this->{$attribute})]);
                if ($user) {
                    $this->addError($attribute, 'This email is already in use.');
                }
            }],
            ['email', 'email'],

        ];
    }

然后使用
setScenario('update)
方法在
actionUpdate()
中设置场景。

Sasha为什么要阻止更新验证?但是,这是一种不好的做法,如果您希望按照自己的方式使用代码:

首先,您需要在模型中创建一个场景

namespace app\models;

use yii\db\ActiveRecord;

class {YourClass} extends ActiveRecord
{
  /** Other stuff above */

public function scenarios(){

  $scenarios = parent::scenarios(); 
  $scenarios['update'] = ['!email', '!password', '!password_confirm'];

 return $scenarios;
 }


public function rules()
    {
        return [
            [['first_name', 'last_name'], 'required'],
            [['email', 'password', 'password_confirm'], 'safe', 'on'=>'update'],
            [['role_id', 'state_id', 'country_id', 'zip'], 'integer'],
            [['first_name', 'last_name', 'email', 'password', 'address', 'city'], 'string', 'max' => 255],
            [['password', 'password_confirm'], 'string', 'min' => 8],

            ['password_confirm', 'compare', 'compareAttribute' => 'password'],
            ['email', function($attribute) {
                $user = static::findOne(['email' => Yii::$app->encrypter->encrypt($this->{$attribute})]);
                if ($user) {
                    $this->addError($attribute, 'This email is already in use.');
                }
            }],
            ['email', 'email'],

        ];
    }

然后使用
setScenario('update)
方法在
actionUpdate()
中设置场景。

Sasha为什么要阻止更新验证?但是,这是一种不好的做法,如果您希望按照自己的方式使用代码:

首先,您需要在模型中创建一个场景

namespace app\models;

use yii\db\ActiveRecord;

class {YourClass} extends ActiveRecord
{
  /** Other stuff above */

public function scenarios(){

  $scenarios = parent::scenarios(); 
  $scenarios['update'] = ['!email', '!password', '!password_confirm'];

 return $scenarios;
 }


public function rules()
    {
        return [
            [['first_name', 'last_name'], 'required'],
            [['email', 'password', 'password_confirm'], 'safe', 'on'=>'update'],
            [['role_id', 'state_id', 'country_id', 'zip'], 'integer'],
            [['first_name', 'last_name', 'email', 'password', 'address', 'city'], 'string', 'max' => 255],
            [['password', 'password_confirm'], 'string', 'min' => 8],

            ['password_confirm', 'compare', 'compareAttribute' => 'password'],
            ['email', function($attribute) {
                $user = static::findOne(['email' => Yii::$app->encrypter->encrypt($this->{$attribute})]);
                if ($user) {
                    $this->addError($attribute, 'This email is already in use.');
                }
            }],
            ['email', 'email'],

        ];
    }

然后使用
setScenario('update)
方法在
actionUpdate()
中设置场景。

Sasha为什么要阻止更新验证?但是,这是一种不好的做法,如果您希望按照自己的方式使用代码:

首先,您需要在模型中创建一个场景

namespace app\models;

use yii\db\ActiveRecord;

class {YourClass} extends ActiveRecord
{
  /** Other stuff above */

public function scenarios(){

  $scenarios = parent::scenarios(); 
  $scenarios['update'] = ['!email', '!password', '!password_confirm'];

 return $scenarios;
 }


public function rules()
    {
        return [
            [['first_name', 'last_name'], 'required'],
            [['email', 'password', 'password_confirm'], 'safe', 'on'=>'update'],
            [['role_id', 'state_id', 'country_id', 'zip'], 'integer'],
            [['first_name', 'last_name', 'email', 'password', 'address', 'city'], 'string', 'max' => 255],
            [['password', 'password_confirm'], 'string', 'min' => 8],

            ['password_confirm', 'compare', 'compareAttribute' => 'password'],
            ['email', function($attribute) {
                $user = static::findOne(['email' => Yii::$app->encrypter->encrypt($this->{$attribute})]);
                if ($user) {
                    $this->addError($attribute, 'This email is already in use.');
                }
            }],
            ['email', 'email'],

        ];
    }

然后使用
setScenario('update)
方法在
actionUpdate()
中设置场景。

电子邮件被用作唯一的登录信息,因此无法更新。密码更新将通过单独的表格处理。因此,我需要在更新时删除电子邮件和密码验证。电子邮件用作唯一的登录信息,因此无法更新。密码更新将通过单独的表格处理。因此,我需要在更新时删除电子邮件和密码验证。电子邮件用作唯一的登录信息,因此无法更新。密码更新将通过单独的表格处理。因此,我需要在更新时删除电子邮件和密码验证。电子邮件用作唯一的登录信息,因此无法更新。密码更新将通过单独的表格处理。因此,我需要删除更新时的电子邮件和密码验证。