Php 验证值不能小于0
我在Yii2上有这张表格Php 验证值不能小于0,php,yii2,yii2-advanced-app,Php,Yii2,Yii2 Advanced App,我在Yii2上有这张表格 Employee Name : N1 Apply Date : 2016-08-26 Start Date : 2016-08-29 Days Leave : 4 Days Days Leave Left : 8 Status : Pending 我期望的结果是,当我输入9天休假时,表单将被验证 "Maximum Quota is Exceeded,Cannot Input" 这是我的模特儿,可以放几天假 pu
Employee Name : N1
Apply Date : 2016-08-26
Start Date : 2016-08-29
Days Leave : 4 Days
Days Leave Left : 8
Status : Pending
我期望的结果是,当我输入9天休假时,表单将被验证
"Maximum Quota is Exceeded,Cannot Input"
这是我的模特儿,可以放几天假
public function daysleaveleft($id,$year) {
$models = Leave::find()->select(['daysleave'=>'sum(daysleave)'])->where(['EmpId' => $id,'year(startdate)'=>$year])->One();
$setting = Setting::find()->where(['Var'=>'MaxLeave'])->One();
return $Setting->Val - $models['daysleave'];
}
已编辑
Days Leave | Days Leave Left
0 | 12
4 | 8
3 | 5
6 | -1
我希望结果是这样的,当用户输入6时,当他还有5个配额时,表单将被验证。您可以使用
在规则中添加:
['field_name', 'integer', 'min' => 1],
['field_name', 'validateLeaveDays', 'when' => $this->startdate && $this->EmpId]
并在您的离开车型中添加:
public function validateLeaveDaysLeft($attribute) {
$year = date('Y', strtotime($this->startdate));
$maxLeaveDays = $this->daysleaveleft($this->EmpId, $year);
if ($this->$attribute < $maxLeaveDays) {
$this->addError($attribute, 'Maximum Quota is Exceeded,Cannot Input');
}
}
daysleaveleft
函数是否在员工模型中?不,它在休假模型中。上面的表单代表哪种模型?它也是休假模型,您如何调用此daysleaveleft
方法?
public function validateLeaveDaysLeft($attribute) {
$year = date('Y', strtotime($this->startdate));
$maxLeaveDays = $this->employee->getLeaveDaysLeft($year);
if ($this->$attribute < $maxLeaveDays) {
$this->addError($attribute, 'Maximum Quota is Exceeded,Cannot Input');
}
}