2016-12-17 1 views
1
[['shortcode'],'string', 'length' => [8, 11]],// first 
['shortcode','required', // second 
       'when' => function($model) { 
        // code here 
       }, 
], 

이 난 단지 내 규칙 length을 결합 수있는 방법 규칙? 미리 감사드립니다.설정 길이 내가 <code>required</code>로 설정하면

현재로서는 항상 최소 길이를 확인하는 중입니다.

답변

2

최상의 옵션 (항상 사용해야 함)은 scenarios을 사용하는 것입니다. 시나리오에 대해서만 다른 솔루션을 모릅니다. 혹시 여기에 당신이 구현 방법은, 아직 사용하지 않은 : 당신이 시나리오 scenario_shortcode_only를 선언 할 때 다른 사람이 무시됩니다 동안

public function scenarios() 
{ 
    return [ 
     'scenario_global' => [<attributes list you want to validate>], 
     'scenario_shortcode_only' => ['shortcode'] 
    ]; 
} 


public function rules() 
{ 
    return [ 
     [['shortcode'], 'string', 'length' => [8, 11], 'on' => 'scenario_shortcode_only'], 
     [['shortcode'], 'required', 'on' => 'scenario_shortcode_only'], 

     // If you need shortcode with different rule set, then use 'except' instead of 'on' 
    ]; 
} 

'on' => 'scenario_shortcode_only' 만 그 규칙이 확인 될 것입니다.