意思是 update 场景不存在,也就是 定义的 rules 中没有该规则:
/** * @inheritdoc * 验证规则 */ public function rules() { return [ [['name', 'password'], 'required', 'on' => ['create'], 'message' => '{attribute}不能为空'], [['password'], 'string', 'max' => 16, 'min' => 6], [['name'], 'string', 'max' => 32, 'on' => ['create'], 'message' => '用户名最大32个字符'], [['screenName'], 'string', 'max' => 32, 'message' => '昵称最大32个字符'], [['name'], 'checkName', 'on' => ['create']], [['screenName'], 'checkName', 'skipOnEmpty' => false], [['name'], 'unique', 'on' => ['create']], [['screenName'], 'unique', 'on' => ['create']], ]; }
现在只需要在相应的字段里边添加一个场景,用来标志相关场景的验证规则
/** * @inheritdoc * 验证规则 */ public function rules() { return [ [['name', 'password'], 'required', 'on' => ['create'], 'message' => '{attribute}不能为空'], [['password'], 'string', 'max' => 16, 'min' => 6], [['name'], 'string', 'max' => 32, 'on' => ['create'], 'message' => '用户名最大32个字符'], [['screenName'], 'string', 'max' => 32, 'message' => '昵称最大32个字符'], [['name'], 'checkName', 'on' => ['create']], [['screenName'], 'checkName', 'skipOnEmpty' => false], [['name'], 'unique', 'on' => ['create']], [['screenName'], 'unique', 'on' => ['create', 'update']], ]; }