简体   繁体   English

内联验证器功能不适用于yii2中的模型

[英]inline validator function not working for model in yii2

I am creating offer for products thats why I have implemented several model at create function in controller 我正在为产品创建报价,这就是为什么我在控制器的创建功能中实现了几种模型的原因

public function actionCreate()
{
    $model = new Offer();
    $wmodel = new Wmoffer();
    $pmodel = new Product();
    $ummodel = new Unitofmeasurement();
    $qvmodel = new OfferingValue();
    $blmodel = new OfferLocation();

   if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    // code manipulation
   }else{
       return $this->render('create', [
            'model' => $model,
            'wmodel' => $wmodel,
            'pmodel' => $pmodel,
            'qvmodel' => $qvmodel,
            'blmodel' => $blmodel,
            'ummodel' => $ummodel
        ]); 
   }

all of my model extends by ActiveRecord aspect Wmoffer() this model looks like as below 我的所有模型都通过ActiveRecord方面Wmoffer()扩展,该模型如下所示

use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
use yii\helpers\FileHelper;

class Wmoffer extends Model
{
   public $bIsProductOrService;
   public $iCatalogueID;
   public $imageProduct;
   public $nHasCurrencyValue;
   public $nHasCurrencyValueMRP;
   public $BusinesslocationIds;


   public function rules() 
   { 
     // validation rules 

Now i need to implement inline validator for start,end date comparison [Start Date should be greater than End Date ] 现在我需要实现内联验证器以进行开始,结束日期比较[开始日期应大于结束日期]

i have tried this and this but this is not working i know something is missing which causes this Any suggestions will be appreciable. 我曾尝试这个这个 ,但是这是行不通的,我知道缺少了什么导致这个任何建议将是明显的。 Thanks 谢谢

You should check $model->errors value after calling $model->validate() to look for validation errors. 在调用$model->validate()查找验证错误之后,应该检查$model->errors值。

Your dates validator method can be something like: 您的日期验证器方法可以是:

public function validateDates($attribute, $params) {
    if ($this->hasErrors()) {
        return;
    }
    if ($this->dateStart > $this->dateEnd)) {
        $this->addError($attribute, 'Start date can not be greater than end date');
    }
}

Add it to rules() in your backend models. 将其添加到后端模型中的rules()中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM