I faced with some problem in TblActiveForm. I have few models in 1 action and I need validate it in ajax. The simple server validation works fine, but I can't do ajax validation. In view file I have 3 tabs each of them display corresponding models.I don't have any error message. Here is my action:
public function actionUpdateModule($id)
{
$model = ModulesInfo::model()->with('rightsInfo')->findbypk($id);
$RI_model = RightsInfo::model()->findAll('module_id=' . $id);
$status = true;
$model_numeration = new ModuleNumeration;
if (isset($_POST['ajax']) && $_POST['ajax'] === 'add_module') {
echo TbActiveForm::validate($model_numeration);
Yii::app()->end();
}
$model_numeration->unsetAttributes();
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
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';
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
));
}
View file:
<?php $form = $this->beginWidget(
'bootstrap.widgets.TbActiveForm',
array(
'layout' => TbHtml::FORM_LAYOUT_HORIZONTAL,
'id' => 'add_module',
'enableAjaxValidation' => true,
'enableClientValidation'=>false,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
),
)); ?>
<fieldset>
<legend><?php echo Yii::t('modules', 'General information'); ?></legend>
<?php echo $form->textFieldControlGroup($model, 'name', array('span' => 6)); ?>
<?php echo $form->textFieldControlGroup($model, 'icon', array('span' => 6)); ?>
<?php echo $form->textFieldControlGroup($model, 'url_name', array('span' => 6)); ?>
<legend></legend>
<label class="control-label" for="RightsInfos">Rights</label>
<div id="RightsInfos">
<?php
if (!empty($model->rightsInfo))
foreach ($model->rightsInfo as $right) {
echo "<div class='controls'>";
echo 'code: ' . TbHtml::textField('RightsInfo[' . $right->id . '][code]', $right->code, array('span' => 1));
echo ' name: ' . TbHtml::textField('RightsInfo[' . $right->id . '][name]', $right->name, array('span' => 4));
echo '</div><br>';
}
?>
<div class="controls">
<?php echo 'code: ' . TbHtml::textField('NewRightsInfo[0][code]', '', array('span' => 1)); ?>
<?php echo 'name: ' . TbHtml::textField('NewRightsInfo[0][name]', '', array('span' => 4)); ?>
<?php echo TbHtml::link('+', $url = '#', array('onclick' => 'javascript:$.registActions();')); ?>
</div>
<br>
</div>
</fieldset>
<?php
if (!isset($model->id))
echo TbHtml::formActions(array(
TbHtml::ajaxButton(Yii::t('common', 'Update'),Yii::app()->controller->createUrl('/Admin/Adminpanel/updateModule',
));
else
echo TbHtml::formActions(array(
TbHtml::ajaxButton(Yii::t('common', 'Update'),Yii::app()->controller->createUrl('/Admin/Adminpanel/updateModule',
));
?>
</div>
<div class="tab-pane" id="code">
<fieldset>
<legend><?php echo Yii::t('modules', 'Code that reference to action') ?></legend>
</fieldset>
<div class="controls">
<?php echo $form->label($model_numeration, 'controller_id', array('class' => 'control-label')); ?>
<?php echo $form->dropDownList($model_numeration, 'controller_id', $controllers, array('empty' => '', 'rows' => '7')); ?>
<?php echo $form->error($model_numeration, 'controller_id', array('class' => 'alert-error'),false); ?>
</div>
<div class="controls">
<?php echo $form->label($model_numeration, 'action_id', array('class' => 'control-label')); ?>
<?php echo $form->dropDownList($model_numeration, 'action_id', array(), array('empty' => '-', 'class' => 'ะตะตะตะต')); ?>
<?php echo $form->error($model_numeration, 'action_id', array('class' => 'alert-error'),false); ?>
</div>
<div class="controls">
<?php echo $form->label($model_numeration, 'code', array('class' => 'control-label')); ?>
<?php echo $form->textField($model_numeration, 'code'); ?>
<?php echo $form->error($model_numeration, 'code', array('class' => 'alert-error'),false); ?>
</div>
<?php
if (!isset ($model->id)) {
echo TbHtml::formActions(array(
TbHtml::ajaxButton(Yii::t('common', 'Update'),Yii::app()->controller->createUrl('/Admin/Adminpanel/updateModule',array('id' => $model->id)),array(), array('color' => '#333333')),
));
} else {
echo TbHtml::formActions(array(
TbHtml::ajaxButton(Yii::t('common', 'Update'),Yii::app()->controller->createUrl('/Admin/Adminpanel/updateModule',array('id' => $model->id)),array(), array('color' => '#333333')),
));
}
?>
</div>
<?php $this->endWidget(); ?>
I think that the problem with your error messages is that in the third parameter of the method error you are setting the enableAjaxValidation to false
Try setting it to true