小数范围的 Yii 验证规则

Suj*_*ibi 5 php validation yii

我想为小数范围值 40.000<= score_percentage <=100.000 创建规则

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

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

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

array('entrance_score', 'compare','operator'=>'>=','compareValue'=>0 , 'message'=>'Minimum Entrance Score should be 40.' ),
Run Code Online (Sandbox Code Playgroud)

Cri*_*sto 2

那么,您想要进行检查以使entrance_score值介于40.000和 之间100.000,对吗?

您可以在模型中放置一个min,max规则,例如:

array('entrance_score', 'numerical', 'integerOnly'=>false, 'min'=>40, 'max'=>100),
Run Code Online (Sandbox Code Playgroud)

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

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

和你的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.');
        }
    }
Run Code Online (Sandbox Code Playgroud)

应该可以。