Michaël
Michaël

Reputation: 1130

Yii model method addError is not working

I have an issue about displaying error in a yii form.

This is my controller:

$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,
  ));
}

This is my index view:

<?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>

Here is my BaseForm

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

Here is the problem:

"$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

Answers (2)

Micha&#235;l
Micha&#235;l

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

Tim Withers
Tim Withers

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

Related Questions