十进制范围
问题描述:
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.'),
答
所以,你要放置一个检查,以便entrance_score
值40.000
和100.000
之间,对不对?
你可以只放置一个min
,max
规则模型,是这样的:
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
我在你的第二条规则中看到了一个拼写错误,''compareValue'=> 0'而不是'expectedValue'=> 40'' –