简体   繁体   English

YII2 自定义验证不适用于自定义函数模型规则

[英]YII2 Custom validation is not working with custom function model rule

I am trying to implement with custom function in model its not working i am not getting what's wrong in my code.我正在尝试在模型中使用自定义函数实现它不起作用我没有发现我的代码有什么问题。 i am trying to call with basic later i will put my condition.我稍后会尝试与基本通话,我会提出我的条件。

Here is model code这是模型代码

public function rules()
    {
        return [
            ['mobile_number', 'required'],
            ['mobile_number', 'myfunction'],

        ];
    }

public function myfunction($attribute,$params)
    {
             $this->addError($attribute, 'You have already submitted');

    }

Here is controller code这是控制器代码

public function actionCreate()
    {
        $model = new Createuser();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

its not assiging the error to a form-field.它没有将错误分配给表单字段。

Thanks in advance.

I guess the parameter named mobile_number does not exist in your data or it is an empty string, so try to add 'skipOnEmpty' => false in your code.我猜您的数据中不存在名为mobile_number的参数,或者它是一个空字符串,因此请尝试在您的代码中添加'skipOnEmpty' => false

Anyway, it works for me.无论如何,它对我有用。

public function rules()
{
    return [
        ['mobile_number', 'required', 'skipOnEmpty' => false],
        ['mobile_number', 'myfunction', 'skipOnEmpty' => false],
    ];
}

are you sure you are not extending the model class ?你确定你没有扩展模型类吗? if so you need to put this :如果是这样你需要把这个:

$model->validate()

Your Model:您的型号:

public function rules()
{
    return [
        ['mobile_number', 'required'],
        ['mobile_number', 'myfunction'],
    ];
}

public function myfunction($attribute,$params)
{
    $this->addError($attribute, 'You have already submitted');
    return false;   
}

And your controller:还有你的控制器:

public function actionCreate()
{
    $model = new Createuser();

    if ($model->load(Yii::$app->request->post()) && $model->save() && $model->validate()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

try this in your controller在你的控制器中试试这个

protected function performAjaxValidation($model = NULL) {

        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            echo json_encode(ActiveForm::validate($model));
            Yii::$app->end();
        }
    }

public function actionCreate()
{
    $model = new Createuser();
    $this->performAjaxValidation($model);
    if ($model->load(Yii::$app->request->post()) && $model->save() && $model->validate()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

Code is fine.代码没问题。 it was error with Dynamic Form validation.动态表单验证出错。

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

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