简体   繁体   English

如何设置全局验证规则-YII2

[英]How Can I Set Global Validation Rule - YII2

I have started with YII2 just today. 我今天才开始使用YII2。 i have installed it successfully and also created required Model, Controllers & views in that using CRUD. 我已经成功安装了它,并使用CRUD在其中创建了所需的模型,控制器和视图。

Now my question is as below: 现在我的问题如下:

I want to create one Global Validator Rule which we can use in throughout the Models in system. 我想创建一个全局验证器规则,我们可以在系统中的所有模型中使用它。

What i have tried so far: 到目前为止我尝试过的是:

For that i have created one new model file(FormValidator) in common folder and generate one custom rule method in it. 为此,我在公用文件夹中创建了一个新的模型文件(FormValidator),并在其中生成了一个自定义规则方法。

common/model/FormValidator.php 通用/模型/ FormValidator.php

namespace common\models;

use Yii;
use yii\base\Model;

class FormValidator extends \yii\db\ActiveRecord {

}

then i tried to extend this model file to my frontend model file. 然后我尝试将此模型文件扩展到我的前端模型文件。 but its not working. 但它不起作用。

frontend/model/Customerprofile.php 前端/模型/ Customerprofile.php

namespace frontend\models;

use Yii;
use common\models\FormValidator;    

class Customerprofile extends FormValidator{

}

It's throw below error message: 它被抛出以下错误消息:

PHP Fatal Error – yii\base\ErrorException

Class 'common\models\FormValidator' not found

I don't know what i have missing in all these process, if anyone of you help me out from this, would be much appreciated! 我不知道我在所有这些过程中缺少什么,如果你们中有人帮助我,将不胜感激!

Thanks in Advance. 提前致谢。

如果要为模型扩展某个主类,则只需扩展CActiveRecord,然后您的模型将扩展此主类。

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

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