我在Yii1中遇到ajax验证问题。如果模型没有错误,则回调方法afterValidate()
不起作用。
这是我的代码:
$form = $this->beginWidget(
'bootstrap.widgets.TbActiveForm',
array(
'id'=> 'add_module',
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'clientOptions' => array(
'validateOnSubmit' => true,
'afterValidate' => 'js: function(form, data, hasError) {
alert(hasError);
}'
),
'layout' => TbHtml::FORM_LAYOUT_HORIZONTAL,
));
如果模型没有错误,则alert(hasError);
无法启动。我该怎么办?
动作:
$model = ModulesInfo::model()->with('rightsInfo')->findbypk($id);
$RI_model = RightsInfo::model()->findAll('module_id=' . $id);
$status = true;
$model_numeration = new ModuleNumeration;
if (Yii::app()->request->getQuery('ModuleNumeration')) {
$model_numeration->scenario = 'search';
$model_numeration->attributes = Yii::app()->request->getQuery('ModuleNumeration');
$status = false;
}
$controllers = $model_numeration->getControllerFromModule($model->url_name);
$moduleNumeration = Yii::app()->request->getPost('ModuleNumeration');
if (isset($_POST['ModulesInfo']) && $moduleNumeration ) {
$model->name = $_POST['ModulesInfo']['name'];
$model->icon = $_POST['ModulesInfo']['icon'];
$model->url_name = $_POST['ModulesInfo']['url_name'];
$model->save();
//check if select controller, if yes validate model ModuleNumeration
if (Yii::app()->request->getPost('ModuleNumeration') &&
Yii::app()->request->getPost('ModuleNumeration')['controller_id'] != false
) {
$model_numeration->attributes = Yii::app()->request->getPost('ModuleNumeration');
$model_numeration->module_id = $model->id;
$model_numeration->scenario = 'update';
//ajax validation odel ModuleNumeration
if(isset($_POST['ajax']) && $_POST['ajax']==='add_module' ){
$model_numeration->validate();
echo TbActiveForm::validate($model_numeration);
Yii::app()->end();
}
if ($model_numeration->validate() && $model_numeration->save()) {
$status = true;
} else {
$status = false;
}
}
if (isset($_POST['RightsInfo']) && is_array($_POST['NewRightsInfo'])) {
foreach ($_POST['RightsInfo'] as $exist_r_id => $exist_r) {
if ($exist_r['code'] == '') {
RightsInfo::model()->deleteByPk($exist_r_id);
} else {
$exist_r_model = RightsInfo::model()->findbypk($exist_r_id);
$exist_r_model->code = $exist_r['code'];
$exist_r_model->name = $exist_r['name'];
$exist_r_model->save();
}
}
}
if (isset($_POST['NewRightsInfo']) && is_array($_POST['NewRightsInfo']) && !empty($_POST['NewRightsInfo'])) {
foreach ($_POST['NewRightsInfo'] as $right) {
if ($right['code'] != "") {
$RI_model = new RightsInfo;
$RI_model->name = $right['name'];
$RI_model->code = $right['code'];
$RI_model->module_id = $id;
$RI_model->save();
}
}
}
ModuleNumeration::setFlashMessage($status);
}
$this->render('addmodule', array(
'model' => $model,
'RI_model' => $RI_model,
'controllers' => $controllers,
'model_numeration' => $model_numeration
));
出现错误时的服务器响应是:
{"ModuleNumeration_action_id":["Action name cannot be blank."],"ModuleNumeration_code":["Code cannot be blank."]}
当没有验证错误时 - html页面但形式未提交
我找到了它无法正常工作的原因。在表格中我有3个模型,其中一个我需要通过ajax验证。此模型中有3个属性是必需的, BUT 在简单的服务器验证中我开始验证此模型 ONLY 当选择第一个属性时,如果没有 - 所有模型都没有需要保存和验证。我做了简单的检查:
if(Yii::app()->request->getPost('ModuleNumeration')['controller_id'] !='')
它运行正常。但是当我尝试在ajax验证中添加此检查时:
if(isset($_POST['ajax']) && $_POST['ajax']==='add_module' ){
if(Yii::app()->request->getPost('ModuleNumeration')['controller_id'] !=''){
echo CActiveForm::validate($model_numeration);
Yii::app()->end();
}
}
这不行。