验证值不能小于 0
Validate a value cannot less than 0
我在 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"
这里是我的模型,请假天数
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 个配额时,表单将生效。
您可以使用 inline validator.
在您的规则中添加:
['field_name', 'integer', 'min' => 1],
['field_name', 'validateLeaveDays', 'when' => $this->startdate && $this->EmpId]
并在您的 Leave
模型中添加:
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
重命名为 getLeaveDaysLeft
或更能描述功能的名称。
- 将
getLeaveDaysLeft
函数移动到员工模型中它应该在的地方。这也将消除传递 id 的需要,因为您可以使用 $this->id
- 确保您的
Leave
模型与员工相关。
这会将您的验证器函数更改为:
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');
}
}
我在 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"
这里是我的模型,请假天数
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 个配额时,表单将生效。
您可以使用 inline validator.
在您的规则中添加:
['field_name', 'integer', 'min' => 1],
['field_name', 'validateLeaveDays', 'when' => $this->startdate && $this->EmpId]
并在您的 Leave
模型中添加:
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
重命名为getLeaveDaysLeft
或更能描述功能的名称。 - 将
getLeaveDaysLeft
函数移动到员工模型中它应该在的地方。这也将消除传递 id 的需要,因为您可以使用$this->id
- 确保您的
Leave
模型与员工相关。
这会将您的验证器函数更改为:
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');
}
}