bryson

bryson

这家伙有点懒,还没写个性签名!

  • 财富值4885
  • 威望值230
  • 总积分8025

个人信息

  • 2016-11-22 已签到
    连续签到2天,获得了10个金钱
  • 因為你的 controller 沒導向到 views

    public function actionAnd(){
        return $this->render('and');
    }
    


    你可以參考 說得把 Table 建成 models,
    既然都選擇 MVC 架構了, 可以去研究一下他的含意

    另外你的辦法也可以, 但是建議你在 Html 上把你所找的值
    用 foreach 的方式 show 出, 程式也比較美觀

  • 2016-11-21 已签到
    连续签到1天,获得了5个金钱
  • 赞了回答

    您的答案 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 錯誤 應該為整數
    
  • 2016-11-18 已签到
    连续签到4天,获得了20个金钱
  • 回复了 的回答

    您的答案 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 錯誤 應該為整數
    

    @paopao2hao
    2-2 不可能都驗證 因為scenario_a 已經設定指驗證 a, b
    2-3 不可能都不驗證 因為scenario_b 已經設定指驗證 a, b, c , 但 b 的rules 是綁在 scenario_a , 所以只會驗證 a, c

    • 剛剛測試過了, 是成功的!! 不知道您是哪輸入出問題!
    • 1 跟 2 的差別在於多了 scenarios 設定請你在確認看看代碼巴
  • 2016-11-17 已签到
    连续签到3天,获得了15个金钱
  • 2016-11-16 已签到
    连续签到2天,获得了10个金钱
  • 2016-11-15 已签到
    连续签到1天,获得了5个金钱
  • 赞了说说
    双十一,公司放歌《我想找一个女朋友》,虐狗啊
副总裁 等级规则
8025/10000
资料完整度
30/100
用户活跃度
0/100

Ta的关注

5

Ta的粉丝

19

Ta的访客

57