2016-03-16 50 views
0

在我的Yii web應用程序中,任何類型的Ajax調用,如Ajax驗證,依賴下拉列表的Ajax等等都不起作用。Ajax在yii中不工作

我的代碼, 在我的表單頁面:

<?php 
          $form = $this->beginWidget('CActiveForm', array(
           'id' => 'workdetails-form', 
           'enableClientValidation' => true, 
           'clientOptions' => array(
            'validateOnChange' => true, 
            'validateOnSubmit' => true, 
           ), 
           // 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. 
           'enableAjaxValidation' => true, 
           'htmlOptions' => array('enctype' => 'multipart/form-data'), 
          )); 
          ?> 

在控制器:

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

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

    if (isset($_POST['Workdetails'])) { 
     $model->attributes = $_POST['Workdetails']; 
     if ($model->validate()) { 
      if ($model->save()) { 
       $this->redirect(array('create')); 
      } 
     } 
    } 

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

對於依賴下拉:

<div class="form-group col-sm-6"> 
           <?php echo $form->label($model, 'designationid', array('class' => 'req')); ?> 
           <?php 
           $designation = CHtml::listData(Designation::model()->findAll(), 'designationid', 'designation_name'); 
           echo $form->dropDownList($model, 'designationid', $designation, array(
            'class' => 'form-control', 
            'prompt' => 'Please Select', 
            'ajax' => array(
             'type' => 'POST', 
             'url' => $this->createUrl('workdetails/Fetchemployee'), // here for a specific item, there should be different URL 
             'update' => '#' . CHtml::activeId($model, 'employeeid'), // here for a specific item, there should be different update 
           'data'=>array('designationid'=>'js:this.value'), 
             ))); 
           ?> 

           <?php echo $form->error($model, 'designationid', array('class' => 'school_val_error')); ?> 
          </div> 

如何解決這個... 請幫我..

+0

稍微調試一下。我會先檢查一下ajax調用是否通過。打開開發人員控制檯並檢查網絡選項卡。如果它不知道你的問題在哪裏。然後把var_dump($ model)放到你的performAjaxValidation函數中,看看到底是什麼。 – peaceman

+0

在開發人員控制檯中,未顯示任何內容。對於依賴下拉列表,同時,下拉列表更改整個頁面被重新加載。這就是爲什麼我說Ajax無法正常工作。 – Arya

回答

1

Arya我與Yii1有同樣的問題,我放棄了使用yii-ajax驗證,因爲我找不到解決這個問題的方法。首先確保你已經初始化/註冊Yii開發的JS文件這些都是

yiiactiveform and yii.js

如果你沒有對你的項目這些文件,這意味着你還沒有註冊他們。要註冊核心JS文件,請在您的main中繼續使用此配置。

'clientScript' => array(
      'scriptMap' => array(
       'jquery.js' => true, 
       'jquery.min.js' => true, 
      ), 
     ), 

或者如果這不起作用,請在標題部分的主視圖中使用此功能。創建表單時

Yii::app()->clientScript->registerCoreScript('jquery'); 

你也可以把它添加到你的基地控制器,它是在組件/ Controller.php這樣

public function init() { 
    parent::init(); 
    Yii::app()->clientScript->registerCoreScript('jquery'); 
} 

在你看來有這個。這將有助於放置錯誤消息。到元素

<?php 
      $form = $this->beginWidget('CActiveForm', array(
       'id' => 'patient-registration-form', 
       'enableClientValidation' => True, 
       'enableAjaxValidation' => FALSE, 
       'clientOptions' => array(
        'validateOnSubmit' => true, 
        'afterValidate' => 'js:function(form, data, hasError) { 
        if(hasError) { 
         for(var i in data) $("#"+i).parent().addClass("has-error"); 
         return false; 
        } 
        else { 
         form.children().removeClass("has-error"); 
         return true; 
        } 
       }', 
        'afterValidateAttribute' => 'js:function(form, attribute, data, hasError) { 

        if(hasError) $("#"+attribute.id).parent().addClass("has-error"); 
         else $("#"+attribute.id).parent().removeClass("has-error"); 
         $("#"+attribute.id).parent().addClass("has-success"); 
       }' 
       ), 
       'htmlOptions' => array(
        'class' => 'form-horizontal form-bordered form-row-stripped', 
       ), 
      )); 
      ?> 

選擇使用Yii2它有固定的很多stufff的,如果你是加載當前頁面使用Ajax,你需要渲染包括的js文件再次整個頁面。因爲當你使用renderPartial時,它不會初始化js文件,因此沒有js腳本可以工作,包括驗證。

+0

通過使用這段代碼,我得到了一個像ReferenceError的錯誤:jQuery沒有定義。我試圖解決這個錯誤,但失敗了。 – Arya

+0

如果它說,比你還沒有定義jQuery(容易檢查:去你的視圖文件做一個腳本標記,並在$(document).ready(function(){alert(「Something!」);})) 。如果頁面重新加載後彈出,則會定義它是否未定義。另一個可能的問題可能是你定義了兩次grep,或者檢查你的views/layout/main.php文件或者你正在渲染這個表單的文件,看它是否被聲明。我有很多實例,我忘記了每次都定義jQuery ... facepalm時刻。 – peaceman