2017-10-19 3 views
1

저는 Yii 프레임 워크에 새로운 기능이 있습니다. 새로운 사용자를 생성하는 작업 양식이 있습니다. 현재 입력 한 사용자의보기가있는보기 페이지로 리디렉션되는 양식이 있습니다. 성공 메시지를 표시 한 후 같은 페이지에 머물러있게하십시오. 나는 AJAX와 함께 만들고 싶다. 당신은 쓰기 양식에서Yii에 AJAX가 포함 된 양식 유효성 확인

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(); 
     } 
    } 


... 
} 

답변

1

: 다음

<?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") 
))); ?> 

자바 스크립트 코드 :

<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> 
여기
<?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)); ?> 

내 컨트롤러 : 여기

내보기입니다
+0

감사합니다. 작동합니다. – user2997418