2017-10-19 7 views
1

Ich bin neu in Yii Rahmen, ich habe ein Arbeitsformular, das einen neuen Benutzer erstellt, derzeit das Formular umleiten zu einer Ansichtsseite, wo es eine Ansicht des eingegebenen Benutzers gibt, möchte ich Bleiben Sie nach dem Anzeigen einer Erfolgsmeldung auf der gleichen Seite. Ich möchte es mit AJAX schaffen.validiere ein Formular mit AJAX in Yii

Hier ist meine Ansicht:

<?php 
/* @var $this UserController */ 
/* @var $model User */ 

$this->breadcrumbs=array(
    'Users'=>array('index'), 
    'Create', 
); 

$this->menu=array(
    array('label'=>'List User', 'url'=>array('index')), 
    array('label'=>'Manage User', 'url'=>array('admin')), 
); 
?> 
<h1>Create User</h1> 
<?php $this->renderPartial('_form', array('model'=>$model)); ?> 

Hier mein Controller ist:

class UserController extends Controller 
{ 
... 

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

     // Uncomment the following line if AJAX validation is needed 
     $this->performAjaxValidation($model); 

     if(isset($_POST['User'])) 
     { 
      $model->attributes=$_POST['User']; 
      if($model->save()) 
       $this->redirect(array('view','id'=>$model->id)); 
     } 


     $this->render('create',array(
      'model'=>$model, 
     )); 

    } 

... 
/** 
    * Performs the AJAX validation. 
    * @param User $model the model to be validated 
    */ 
    protected function performAjaxValidation($model) 
    { 
     if(isset($_POST['ajax']) && $_POST['ajax']==='user-form') 
     { 
      echo CActiveForm::validate($model); 
      Yii::app()->end(); 
     } 
    } 


... 
} 

Antwort

1

In dem Formular schreiben Sie:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'user-form', 
    // Please note: When you enable ajax validation, make sure the corresponding 
    // controller action is handling ajax validation correctly. 
    // There is a call to performAjaxValidation() commented in generated controller code. 
    // See class documentation of CActiveForm for details on this. 
    /*updated by Ismail*/ 
    'enableAjaxValidation'=>true, 
    'clientOptions'=>array(
     'validateOnSubmit'=>true, 
     'afterValidate' => 'js:function(form, data, hasError) { 
      if(hasError) { 
       return false; 
      } 
      else 
      { 
      // return true; it will submit default way 
        ajaxSubmitHappen(form, data, hasError); //and it will submit by ajax function 
       } 
      }', 
     'htmlOptions'=>array('role'=>"form") 
))); ?> 

Dann wird der JavaScript-Code:

<script> 
     function ajaxSubmitHappen(form, data, hasError) { 
      if (!hasError) { 
       $.ajax({ 
        "type": "POST", 
        "url": "<?php echo $url; ?>", 
        "data": form.serialize(), 
        "success": function (data) { 
         $('#successMessage').html('<div class="alert alert-success"><strong>Success!</strong> User added successfully. </div>'); 
         setTimeout('$("#successMessage").fadeOut("slow")', 2000); 
        } 
       }); 
       /*reset form after submitting*/ 
       $("#user-form")[0].reset(); 
      } 
      else { 
       $('#successMessage').html('<div class="alert alert-danger"><strong>Error!</strong> An error has occured, please try again. </div>'); 
       setTimeout('$("#successMessage").fadeOut("slow")', 2000); 
      } 
     } 
    </script> 
+0

Danke, es funktioniert jetzt – user2997418

Verwandte Themen