Reputation: 1130
I have an issue about displaying error in a yii form.
$custom_user->attributes = $_POST['CustomUser'];
if($custom_user->validate())
{
...
save
...
}
else
{
$custom_user->addError('username', 'Error X');
$this->redirect(array('access/index'),
array('user'=>$custom_user,
'tab'=>$tab_person,
));
}
<?php $form=$this->beginWidget('BaseForm', array(
'id'=>'user-form',
'action'=>Yii::app()->createUrl('person/createUser'))); ?>
<?php echo $form->errorSummary($user);?>
<div class="right">
<?php echo $form->textField($user,'username',array('size'=>30,'maxlength'=>255)); ?>
<br/><?php echo $form->error($user,'username'); ?>
</div>
...
...
<div style="float: left;">
<?php echo $form->dateField($user,'valid_from',null,'valid_from_formated'); ?>
<br/><?php echo $form->error($user,'valid_from'); ?>
</div>
public $enableClientValidation = true;
public $enableAjaxValidation = true;
public $clientOptions = array( 'hideErrorMessage'=>false,
'validateOnSubmit'=>true,
'validateOnChange'=>false,
'validateOnType'=>false,
'afterValidateAttribute' => 'js:enableSubmitAV',
'afterValidate' => 'js:submitFormAV'); // always fires this after
"$form->errorSummary($user);" is empty and noting is display under "$form->error($user,'username');"
But if I look in Firebug I can see that under "response" tab:
{"CustomUser_valid_until":["Valid Until must be greater than \"20121127\"."]}
it's a good thing because it means my rules work great. But none errors are displayed. Not this one and even the error "Error X" I add in my controller is not diplayed... (I'm sure I pass throught the else statement, i tried it).
So, does anyone could have an idea?
Thanks for reading me and sorry for my approximate English.
Have a good day :)
Michaël
Upvotes: 1
Views: 4425
Reputation: 1130
Ok finally I got what was wrong. It was the configuration in my CActiveForm. The option "enableAjaxValidation" seems to make some problem when it's on "true". I don't know really why but I've already had this problem with a login form befor. I'll try to work out this problem.
Upvotes: 0
Reputation: 12059
You are redirecting, meaning a whole new request is called, and any responses are deleted.
Why are you redirecting to "access/index" rather than just calling the view? If you do need to redirect, which you definitely can, try setting a flash variable as it is store in the browser's session and will persist through redirects:
Yii::app()->user->setFlash('username', "Error X");
Then on the access/index page:
<?php if(Yii::app()->user->hasFlash('username')):?>
<div class="error">
<?php echo Yii::app()->user->getFlash('username'); ?>
</div>
<?php endif; ?>
Upvotes: 1