paopao2hao
- paopao2hao 2017-11-16 已签到连续签到1天,获得了5个金钱
- paopao2hao 收藏了教程Yii2.0 场景的简单使用
- paopao2hao 2016-11-21 已签到连续签到1天,获得了5个金钱
- paopao2hao 回复了 bryson 的回答
您的答案 C 是會驗證的 , 欄位跟場景是不同的
下面舉些例子看看差別
首先預設參數
$arr = [ 'a' => 'string', 'b' => 'string', 'c' => 'default' ];
單驗證欄位
1.public function rules() { return [ [['a'], 'string'], [['b', 'c'], 'integer'] ]; } $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 驗證 c 錯誤 應該為整數
加入場景 scenario_a , scenario_b
1.
public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } $model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 正確 因為只驗證 a,c 欄位 驗證 c 錯誤 應該為整數
1-1.
public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } $model->scenario = 'scenario_a'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 因為場景選擇 scenario_a 驗證 c 錯誤 應該為整數
指定驗證欄位
2.public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } public function scenarios() { $scenarios = parent::scenarios(); $scenarios['scenario_a'] = ['a', 'b']; 場景 scenario_a 只驗證 a,b 欄位 $scenarios['scenario_b'] = ['a', 'b', 'c']; 場景 scenario_b 只驗證 a,b,c 欄位 return $scenarios; }
2-1
$model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 錯誤 應該為整數 驗證 c 錯誤 應該為整數
2-2
$model->scenario = 'scenario_a'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 未驗證 c
2-3
$model->scenario = 'scenario_b'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 因為 b 屬於場景 scenario_a 驗證 c 錯誤 應該為整數
不好意思,仔细检查了半天发现是我的scenario方法名少了个s,您的结论是对的,受益匪浅!!!
- paopao2hao 回复了 bryson 的回答
您的答案 C 是會驗證的 , 欄位跟場景是不同的
下面舉些例子看看差別
首先預設參數
$arr = [ 'a' => 'string', 'b' => 'string', 'c' => 'default' ];
單驗證欄位
1.public function rules() { return [ [['a'], 'string'], [['b', 'c'], 'integer'] ]; } $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 驗證 c 錯誤 應該為整數
加入場景 scenario_a , scenario_b
1.
public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } $model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 正確 因為只驗證 a,c 欄位 驗證 c 錯誤 應該為整數
1-1.
public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } $model->scenario = 'scenario_a'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 因為場景選擇 scenario_a 驗證 c 錯誤 應該為整數
指定驗證欄位
2.public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } public function scenarios() { $scenarios = parent::scenarios(); $scenarios['scenario_a'] = ['a', 'b']; 場景 scenario_a 只驗證 a,b 欄位 $scenarios['scenario_b'] = ['a', 'b', 'c']; 場景 scenario_b 只驗證 a,b,c 欄位 return $scenarios; }
2-1
$model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 錯誤 應該為整數 驗證 c 錯誤 應該為整數
2-2
$model->scenario = 'scenario_a'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 未驗證 c
2-3
$model->scenario = 'scenario_b'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 因為 b 屬於場景 scenario_a 驗證 c 錯誤 應該為整數
@bryson 您好,最近在学习场景,看到你的评论很受益,我按照的你例子测试了一下,跟你的结果有很大出入,请指教。我的代码如下:
TestModel.php
<?php
namespace frontend\models;
use yii\base\Model;
class TestModel extends Model
{public $a; public $b; public $c; public function rules(){ return [ [['a'], 'integer'], [['b'], 'integer', 'on' => 'scenario_b'], [['c'], 'integer'] ]; } public function scenario(){ $scenarios = parent::scenarios(); $scenarios['scenario_a'] = ['a', 'b']; $scenarios['scenario_b'] = ['a', 'b', 'c']; return $scenarios; }
}
TestController.php
<?php
namespace frontend\controllers;
use yii\web\controller;
use frontend\models\TestModel;
class TestController extends Controller{public function actionTest(){ $model=new TestModel; $arr=[ 'a' => 'string', 'b' => 'string', 'c' => 'default' ]; $model->attributes=$arr; print_r($model->attributes); $model->scenario='scenario_b'; if($model->validate()){ echo '验证通过'; }else{ echo '验证未通过';print_r($model->errors); } }
}
不知道我的测试代码是否正确?请指点 - paopao2hao 回复了 bryson 的回答
您的答案 C 是會驗證的 , 欄位跟場景是不同的
下面舉些例子看看差別
首先預設參數
$arr = [ 'a' => 'string', 'b' => 'string', 'c' => 'default' ];
單驗證欄位
1.public function rules() { return [ [['a'], 'string'], [['b', 'c'], 'integer'] ]; } $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 驗證 c 錯誤 應該為整數
加入場景 scenario_a , scenario_b
1.
public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } $model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 正確 因為只驗證 a,c 欄位 驗證 c 錯誤 應該為整數
1-1.
public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } $model->scenario = 'scenario_a'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 因為場景選擇 scenario_a 驗證 c 錯誤 應該為整數
指定驗證欄位
2.public function rules() { return [ [['a'], 'string'], [['b'], 'integer', 'on' => 'scenario_a'], 只讓場景 scenario_a 驗證 [['c'], 'integer'] ]; } public function scenarios() { $scenarios = parent::scenarios(); $scenarios['scenario_a'] = ['a', 'b']; 場景 scenario_a 只驗證 a,b 欄位 $scenarios['scenario_b'] = ['a', 'b', 'c']; 場景 scenario_b 只驗證 a,b,c 欄位 return $scenarios; }
2-1
$model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 錯誤 應該為整數 驗證 c 錯誤 應該為整數
2-2
$model->scenario = 'scenario_a'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 驗證 b 錯誤 應該為整數 未驗證 c
2-3
$model->scenario = 'scenario_b'; $model->attributes = $arr; $model->validate(); 驗證 a 正確 未驗證 b 因為 b 屬於場景 scenario_a 驗證 c 錯誤 應該為整數
你这个代码测试过吗,我测试的结果2-2和2-3好像是错的。2-2的结果是:abc都验证。2-3的结果是abc都不验证
- paopao2hao 2016-11-17 已签到连续签到1天,获得了5个金钱