2016-11-03 10 views
1

Ich habe eine Form, die in Popup öffnen wird, damit ich von Ajax Validierung meine Form überprüfen möchten, aber wenn ich auf den Knopf meiner Seite einreichen klicken Sie immer aktualisiert, so bin ich keine Fehler bei der Überprüfung immerValidate Form von Ajax in yii2

View file:

<?php $form = ActiveForm::begin([ 
     'id' => 'signup-form', 
     'enableAjaxValidation' => true, 
     //'action' => Url::toRoute('user/ajaxregistration'), 
     'validationUrl' => Url::toRoute('user/ajaxregistration') 

]); ?> 

<div class="col-md-12"> 
        <div class="formbox"> 
         <div class="inputbox signup"> 
         <div class="input-group"> <span class="input-group-addon"><i class="glyphicon name"></i></span> 
          <?= Html::textInput('userFullName', '', ['placeholder' => "Name",'class'=>'form-control']); ?> 
         </div> 

<?php ActiveForm::end(); ?> 

Controller-Datei:

public function actionValidate() { 

    $model = new SignupForm; 

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

    } 


} 

Model Code:

return [ 

      ['userFullName', 'trim'], 
      ['userFullName', 'required'], 


     ]; 

Bitte mir vorschlagen, was soll ich tun, damit meine Seite nicht refrsh bekommen, und ich werde den Validierungsfehler

Antwort

1

Verwendung renderAjax() Methode erhalten:

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

}else { 
      return $this->renderAjax('YourViewFileName', [ 
       'model' => $model, 
      ]); 
     } 
1

Sie verwenden ein Activeform ohne ActiveFields bedeutet, dass die Validierungsregeln, die im Modell definiert wurden, nicht einmal der Texteingabe zugewiesen werden.

Modell:

use Yii; 
use yii\base\Model; 

class ExampleForm extends Model 
{ 
    // this 'virtual attribute' defines a form field 
    public $userFullName; 

    // validation rules 
    public function rules() 
    { 
     return [ 
      ['userFullName', 'trim'], 
      ['userFullName', 'required'], 
     ]; 
    } 

} 

Controller:

use models\ExampleForm; 
use yii\web\Response; 
use yii\widgets\ActiveForm; 

public function actionExample() 
{ 
    $model = new ExampleForm; 

    // validate any AJAX requests fired off by the form 
    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { 
     Yii::$app->response->format = Response::FORMAT_JSON; 
     return ActiveForm::validate($model); 
    } 

    if ($model->load(Yii::$app->request->post())) { 

     // code to process form data goes here.. This will execute on a form submission. 

    } 

    return $this->render('example-form', [ 
     'model' => $model, 
    ]); 
} 

Ansicht: Ich habe eine Implementierung für Ihr Problem geschrieben

<?php 
use yii\widgets\ActiveForm; 
use yii\helpers\Html; 

$this->title = 'Example'; 
?> 
<div class="exampleForm"> 

    <h1><?= Html::encode($this->title) ?></h1> 

    <p>Please fill out the form.</p> 

    <!-- this form will submit via POST to the same action that renders it --> 
    <?php $form = ActiveForm::begin() ?> 

     <!-- this is an active field. Any validation rules for the 'userFullName' field --> 
     <!-- that have been defined within the $model object will be applied to this field --> 
     <!-- the validation has also been set to validate via ajax within the $options array --> 
     <!-- read more about ActiveFields here: http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html --> 
     <?= $form->field($model, 'userFullName', ['enableAjaxValidation' => true]) ?> 

     <div class="form-group"> 
      <?= Html::submitButton('Submit!', ['class' => 'btn btn-primary']) ?> 
     </div> 

    <?php ActiveForm::end(); ?> 

</div> 

Der beste Weg, um zu sehen, ob AJAX-Anfragen sein werden Gesendet/Das Formular wird validiert, um die Chrome-Entwickler-Tools zu überprüfen, auf die Registerkarte "Netzwerk" und auf "Inspizieren" zu gehen ct die Aktivität.