在我的Yii Web应用程序中,任何类型的Ajax调用,如Ajax验证,Ajax依赖下拉等......都无法正常工作。
我的代码是, 在我的表单页面中:
<?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 :(得分:1)
Arya我和Yii1有同样的问题,我放弃使用yii-ajax验证,因为我找不到解决方法。首先确保初始化/注册Yii-js文件
yiiactiveform和yii.js
如果您的项目中没有这些文件,则表示您尚未注册这些文件。要注册核心JS文件,请在主服务器中继续此配置。
'clientScript' => array(
'scriptMap' => array(
'jquery.js' => true,
'jquery.min.js' => true,
),
),
或者如果不起作用,请在标题部分的主视图中使用它。
Yii::app()->clientScript->registerCoreScript('jquery');
您也可以将它添加到位于components / 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它已经修复了很多东西,如果你用ajax加载当前页面,你需要再次渲染包括js文件在内的整个页面。因为当你使用renderPartial时它不会初始化js文件,因此没有js脚本可以工作,包括验证。