2015-09-05 67 views
3

Yii的验证规则我想创造十进制范围值40.000 < = score_percentage <规则= 100.000十进制范围

array('entrance_score', 'compare','operator'=>'<=','compareValue'=>100, 'message'=>'Maximum Entrance Score should be 100.'), 

虽然从GUI测试,它使上可接受的小于100的十进制数,但不能使上可接受的大于或等于40.000。

以下规则工作不正常,我该怎么办?

array('entrance_score', 'compare','operator'=>'>=','compareValue'=>0 , 'message'=>'Minimum Entrance Score should be 40.'), 
+0

我在你的第二条规则中看到了一个拼写错误,''compareValue'=> 0'而不是'expectedValue'=> 40'' –

回答

1

所以,你要放置一个检查,以便entrance_score40.000100.000之间,对不对?

你可以只放置一个minmax规则模型,是这样的:

array('entrance_score', 'numerical', 'integerOnly'=>false, 'min'=>40, 'max'=>100), 

,或者你可以创建自己的自定义的验证规则,如:

public function rules() 
    { 
     return array(
      //............ 
      array('entrance_score', 'numerical', 'integerOnly'=>false), 
      array('entrance_score', 'authenticate'), 
      //..... 
     ); 
    } 

和你authenticate功能:

public function authenticate($attribute,$params) 
    { 
     if($this->entrance_score < 40) { 
      $this->addError('entrance_score','Minimum Entrance Score should be 40.'); 
     } elseif($this->entrance_score > 100) { 
      $this->addError('entrance_score','Maximum Entrance Score should be 100.'); 
     } 
    } 

那肩d做到这一点。

+0

'integer'而不是'Yii2'的'numerical'。 – nicolascolman

相关问题