0

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.
2
  • I'm having the same problem in my application, did you solved it? Have any ideas? Commented Feb 5, 2016 at 13:19
  • yes @Clyff i fixed that Commented Apr 27, 2016 at 6:52

5 Answers 5

3

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.

Anyway, it works for me.

public function rules()
{
    return [
        ['mobile_number', 'required', 'skipOnEmpty' => false],
        ['mobile_number', 'myfunction', 'skipOnEmpty' => false],
    ];
}
Sign up to request clarification or add additional context in comments.

Comments

0

are you sure you are not extending the model class ? if so you need to put this :

$model->validate()

Comments

0

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,
        ]);
    }
}

2 Comments

please enter first mobile number and then submit form then you can show error msg
i need to show while submitting to a form-field in this form also reseting. and pjax is not working.
0

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,
        ]);
    }
}

Comments

0

Code is fine. it was error with Dynamic Form validation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.