1

I have ajax validation on unique on create it work fine but when I want to update i cant becouse this show message that this name is already used but i not owerwrite this name it is name from database on update click. Its does not matter whcich record i want to update it always show me message that name is already used. Whan can i do to disable message when i not change my input to name which is in base. Now it is so update action automatically filed my inputs but when i not change anythink i have this error on save

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], 'id'=>$model->formName(), 'enableAjaxValidation'=>true, 'validationUrl'=>Url::toRoute('category/validation')]) ?>

My controller:

 public function actionValidation(){
        $model= new SmCategory;
        if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
        {
            Yii::$app->response->format='json';
            return ActiveForm::validate($model);
        }
    }

my rules:

public function rules()
    {
        return [
            [['Name'], 'required'],
            ['Name', 'unique', 'targetClass' => 'common\models\Smcategory', 'message' => 'This name has already been taken.'],
            [['Rel_Category', 'IsDeleted'], 'integer'],
            [['File'],'file'],
            [['Name', 'Label'], 'string', 'max' => 45],
            [['Picture'], 'string', 'max' => 255]
        ];
    }
qwerty
  • 101
  • 1
  • 10
  • add [scenario](http://www.yiiframework.com/doc-2.0/yii-base-model.html#scenarios%28%29-detail) in model rules. and refer: http://stackoverflow.com/questions/31252747/yii2-scenarios-model-method – Insane Skull Feb 29 '16 at 09:32
  • i tried to do scenarios but i want to make this unique on create and update actions – qwerty Feb 29 '16 at 09:34
  • can u show me example plis i new in Yii and i dont have idea how to do this i tried with this scenarios but it still not work for me – qwerty Feb 29 '16 at 09:51
  • add `['Name', 'unique', 'targetClass' => 'common\models\Smcategory', 'message' => 'This name has already been taken.', 'on' => 'create'],` and in controller add `$model->scenario = 'create';` – Insane Skull Feb 29 '16 at 09:56
  • but then on update it not work i want to have this rule on update too – qwerty Feb 29 '16 at 10:03
  • i want to have this validation on update and create but when i update this validation it shouldnt show me error when i not change my atributes. Now on update i have on start filled all fields from database and when i dont change anything i have error. It shoudlnt work that – qwerty Feb 29 '16 at 10:15
  • have you tried above solution, it will work for both. – Insane Skull Feb 29 '16 at 10:27
  • yes i tried then ajax validation not work on create it work only when i clicked button and on update this validation not work on yii2 and ajax too – qwerty Feb 29 '16 at 10:43
  • You don't need scenario here – soju Feb 29 '16 at 11:01

1 Answers1

2

The problem is here :

$model= new SmCategory;

This code is ok for create, not for update since it will not use the existing model for validation, it could be (just an example and assuming id is the primary key) :

public function actionValidation($id = null)
{
    $model = $id===null ? new SmCategory : SmCategory::findOne($id);
    if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
    {
        Yii::$app->response->format='json';
        return ActiveForm::validate($model);
    }
}

And you could update validationUrl in your view :

$validationUrl = ['category/validation'];
if (!$model->isNewRecord)
    $validationUrl['id'] = $model->id;

$form = ActiveForm::begin([
    'options' => ['enctype' => 'multipart/form-data'],
    'id' => $model->formName(),
    'enableAjaxValidation' => true,
    'validationUrl' => $validationUrl,
]);
soju
  • 25,111
  • 3
  • 68
  • 70