1

我需要比较模型中的 2 个属性值,并且只有当第一个值低于第二个值时才能验证。我尝试使用下面的代码,但它不起作用。

控制器

public function actionOpanningBalance(){

     $model = new Bill();
     if ($model->load(Yii::$app->request->post())) {
        $model->created_at = \Yii::$app->user->identity->id;
        $model->save();
      }else{
       return $this->render('OpanningBalance', [
            'model' => $model,
        ]);
     } 
}

模型

public function rules()
{
    return [
        [['outlet_id', 'sr_id', 'bill_number', 'bill_date', 'created_at', 'created_date','bill_amount','credit_amount'], 'required'],
        [['outlet_id', 'sr_id', 'created_at', 'updated_at'], 'integer'],
        [['bill_date', 'd_slip_date', 'cheque_date', 'created_date', 'updated_date','status'], 'safe'],
        [['bill_amount', 'cash_amount', 'cheque_amount', 'credit_amount'], 'number'],
        [['comment'], 'string'],
        ['credit_amount',function compareValue($attribute,$param){
               if($this->$attribute > $this->bill_amount){
               $this->addError($attribute, 'Credit amount should less than Bill amount');
    }],           
        [['bill_number', 'd_slip_no', 'bank', 'branch'], 'string', 'max' => 225],
        [['cheque_number'], 'string', 'max' => 100],
        [['bill_number'], 'unique']
    ];
}
}

它进入验证器功能,但没有像我想要的那样添加错误

$this->addError($attribute, '贷方金额应小于账单金额');

任何人都可以帮助我吗?

4

5 回答 5

3

如果验证没有添加任何错误,则很可能会被跳过。这个问题很可能是因为默认规则行为,它跳过空的或已经给出错误的值,如下所示:https ://www.yiiframework.com/doc/guide/2.0/en/input-validation#inline-validators

具体来说:

默认情况下,如果内联验证器的关联属性接收到空输入或它们已经未通过某些验证规则,则不会应用内联验证器。如果您想确保始终应用规则,您可以在规则声明中将 skipOnEmpty 和/或 skipOnError 属性配置为 false。

因此,您需要根据适合您的方式设置skipOnEmptyor值:skipOnError

[
    ['country', 'validateCountry', 'skipOnEmpty' => false, 'skipOnError' => false],
]
于 2018-03-26T01:39:09.920 回答
2

尝试这个:

public function actionOpanningBalance(){

 $model = new Bill();
 if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    $model->created_at = \Yii::$app->user->identity->id;
    $model->save();
  }else{
   return $this->render('OpanningBalance', [
        'model' => $model,
    ]);
 } 
}

用于验证

您可以使用匿名函数:

['credit_amount',function ($attribute, $params) {
            if ($this->$attribute > $this->bill_amount)) {
                $this->addError($attribute, 'Credit amount should less than Bill amount.');
              return false;
            }
        }],
于 2015-12-02T11:27:34.133 回答
0

这是一个后端验证,它只会在提交时触发。所以你可以在你的验证函数中尝试这样的事情。

if (!$this->hasErrors()) {
  // Your validation code goes here.
}

如果您检查basic Yii2生成的应用程序,您可以在文件中看到该示例models/LoginForm.php,其中有一个名为validatePassword.

只有在提交表单后才会触发验证。

于 2020-07-02T11:16:49.693 回答
0

你可以像这样使用下面的答案也是写

public function rules(){
    return [
        ['credit_amount','custom_function_validation', 'on' =>'scenario'];
}
public function custom_function_validation($attribute){
    // add custom validation
    if ($this->$attribute < $this->cash_amount) 
        $this->addError($attribute,'Credit amount should less than Bill amount.');
}
于 2015-12-02T13:02:23.487 回答
0

我已经使用第三个参数使 custom_function_validation 工作,如下所示:

public function is18yo($attribute, $params, $validator)
{
    $dobDate = new DateTime($this->$attribute);
    $now = new DateTime();
    if ($now->diff($dobDate)->y < 18) {
        $validator->addError($this, $attribute, 'At least 18 years old');
        return false;
    }
}
于 2019-05-24T13:42:34.380 回答