2016-05-07 8 views
1

Ich sehe einige Links sagen, dass Yi2 eindeutige Validierung nur für Ajax-Validierung ist (Aber ich finde keine Best Practice zu folgen.). Also ich frage dies für die Nicht-Ajax-Validierung in Yii2yii2 Unique Validation (non-ajax) funktioniert nicht

mein Modell Regeln sind

['name', 'unique', 'targetAttribute' => 'function_id','message' => 'name must be unique.'], 

Meine Controller-Funktionen normale actionCreate sind()

Anybody Lösung dafür? Vielen Dank im Voraus

Edit: Für Ajax ok.I Hat, Mein Controller-Code jetzt

$model = new JobFunctionRole(); 
    $model->scenario = 'insert'; 

    $jobFunction=JobFunction::find()->all(); 
    if(Yii::$app->request->post()){ 
     if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { 
      Yii::$app->response->format = Response::FORMAT_JSON; 
      return ActiveForm::validate($model); 
     } 
     if ($model->save()) { 
      return $this->redirect(['view', 'id' => $model->job_function_role_id]); 
     } 
    } else { 
     return $this->render('create', [ 
      'model' => $model,'jobFunction'=>$jobFunction 
     ]); 
    }` 

Aber Seite umleitet und zeigt leere Seite.

+0

Tun Sie es in Ajax Validierung überprüfen müssen. Ich werde dich führen. –

+0

ok.Ich habe, Mein Controller Code ist jetzt: Siehe Edits in meiner Frage. Danke! :) – Jackhad

+0

Conceptually, auf welche Weise Sie function_id für Scheckeindeutigkeit verwenden? – scaisEdge

Antwort

0

Könnte man für beide togheter

['a1', 'unique', 'targetAttribute' => ['a1', 'a2']] 


[ 
    'name', 'unique', 
    'targetAttribute' => ['name','function_id'], 
     'message' => 'name must be unique.' 
], 
+0

Danke !! Funktioniert gut. Aber warum ist es nicht für Nicht-Ajax-Aktionen verfügbar? – Jackhad

+0

Validierung funktioniert, aber Seite umleiten auf leere Seite .. :( – Jackhad

+0

Während ... der Validierung? Oder nach der Validierung nach dem Festschreiben)? – scaisEdge