Yii的默认ajax无效

时间:2016-04-02 04:16:38

标签: ajax yii

在我的Yii应用程序中,Yii的默认Ajax无效。默认的Ajax验证也不起作用。这是安装问题还是其他任何问题。如何启用Yii的默认Ajax。 在我的控制器中,

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

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

    if (isset($_POST['Company'])) {
        $company = Company::model()->findAll();
        if (count($company) === 0) {
            $model->attributes = $_POST['Company'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            if (isset($uploadedFile)) {
                $fileName = date('Ymdhis') . '_' . $uploadedFile->name;  // $timestamp + file name
                $model->logo = $fileName;
            }
            if ($model->validate()) {
                if ($model->save()) {
                    if (isset($uploadedFile)) {
                        $uploadedFile->saveAs(Yii::app()->basePath . '/../banner/' . $fileName);
                    }
                    $this->redirect(array('create'));
                }
            }
        } else {
            Yii::app()->user->setFlash('error', 'Company details is already exists.');
        }
    }

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

在视图页面中,

  <?php
                $form = $this->beginWidget('CActiveForm', array(
                    'id' => 'company-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'),
                ));
                ?>

 <div class="form-group">
                    <?php echo $form->label($model, 'company_name', array('class' => 'req')); ?>
                    <?php echo $form->textField($model, 'company_name', array('class' => 'form-control')); ?>
                    <?php echo $form->error($model, 'company_name', array('class' => 'school_val_error')); ?>
                </div>

请帮帮我。

谢谢...

1 个答案:

答案 0 :(得分:0)

Yii没有默认AJAX。这是一种基于JavaScript语言的技术。默认情况下,Yii包含一个jQuery库,它提供了一些使用AJAX轻松操作的方法。如果要在页面上使用它,则应添加以下字符串:

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

您可以将此字符串添加到主布局中,例如添加到/ views / layouts / main.php`的顶部