$model->addError doesn't not work

Hi,

im having a problem with $model->addError method,

in the view error not show

NOTE that both conditions are lead to addError


public function actionWithdraw()

	{

		require_once(dirname(__FILE__).'/../components/paypal/CPayPal.php');

		$model = new PendingWithdraw;

		$model->userid = Yii::app()->user->id;

		if(isset($_POST['PendingWithdraw']))

		{

			$model->attributes=$_POST['PendingWithdraw'];

			$model->addError('amount', 'You don\'t have <strong>'.$model->amount.'&euro;</strong> in your account.');

			$email = NULL;

			$isvalidemail = Payment::userHavePaymentEmail($model->paywith, $email);

			if(!$isvalidemail)

				$model->addError('paywith', 'You dont have an email account for the requested payment.');

			else

				$model->email = $email;

			$money = User::myGetMoney();

			if($model->amount > $money)

			{

				$model->addError('amount', 'You don\'t have <strong>'.$model->amount.'&euro;</strong> in your account.');

			}

			if($model->validate())

			{

				die('ok');

			}

		}

		$data = array('model'=>$model);

		$this->render('withdraw', $data);

	}

my view:


<?php

$this->pageCaption='Withdraw';

$this->pageTitle=Yii::app()->name.' - '.$this->pageCaption;

$this->pageDescription='cashout funds';

?>


<div class="form">


<?php $form=$this->beginWidget('BActiveForm', array(

    'id'=>'pending-withdraw-form',

    'enableAjaxValidation'=>false,

)); ?>


    <?php $this->widget('BAlert',array(


        'content'=>'<p>Fields with <span class="required">*</span> are required.</p><p>You must have an email to the requested payment</p>'

    )); ?>


    <?php echo $form->errorSummary($model); ?>


    <div class="<?php echo $form->fieldClass($model, 'amount'); ?>">

        <?php echo $form->labelEx($model,'amount'); ?>

        <div class="input">

        <div class="input-append">

            <?php echo $form->textField($model,'amount', array('maxlength'=>6,

																'class'=>'span2 validate[required,custom[number],min['.PendingWithdraw::$_Options['min'].'],max['.PendingWithdraw::$_Options['max'].']]')); ?>

            <span class="add-on"><strong>&euro;</strong></span>

            <?php echo $form->error($model,'amount'); ?>

        </div>

        </div>

    </div>


    <div class="<?php echo $form->fieldClass($model, 'paywith'); ?>">

        <?php echo $form->labelEx($model,'paywith'); ?>

        <div class="input">

            <?php echo $form->dropDownList($model,'paywith', Payment::generateUserWithdrawPayWith(), array('class'=>'span8')); ?>

            <?php echo $form->error($model,'paywith'); ?>

        </div>

    </div>




    <div class="actions">

        <?php echo BHtml::submitButton('Submit'); ?>

    </div>


<?php $this->endWidget(); ?>


</div>


<?php Yii::app()->clientScript->registerScript('pending-withdraw-form-validator','$("#pending-withdraw-form").validationEngine();'); ?>

this model is a CActiveRecord

Thanks

You are using a custom activeform (BActiveForm)… try with the standard one (CActiveForm) to see first if that works…

not solve the problem

i have added some var_dumps to test and see the result:


else if(isset($_POST['PendingWithdraw']))

		{

			$model->attributes=$_POST['PendingWithdraw'];

			$model->userid = Yii::app()->user->id; // No hacking

			$model->addError('amount', 'You don\'t have <strong>'.$model->amount.'&euro;</strong> in your account.');

			var_dump($model->getErrors());

			$email = NULL;

			$isvalidemail = Payment::userHavePaymentEmail($model->paywith, $email);

			if(!$isvalidemail)

				$model->addError('paywith', 'You dont have an email account for the requested payment.');

			else

				$model->email = $email;

			$money = User::myGetMoney();

			if($model->amount > $money)

			{

				$model->addError('amount', 'You don\'t have <strong>'.$model->amount.'&euro;</strong> in your account.');

			}

			var_dump($model->getErrors());

			$model->email = 'email@email.com';

			if($model->validate())

			{

				die('ok');

			}

			var_dump($model->getErrors());

		}

view:


array

  'amount' => 

    array

      0 => string 'You don't have <strong>10&euro;</strong> in your account.' (length=57)

array

  'amount' => 

    array

      0 => string 'You don't have <strong>10&euro;</strong> in your account.' (length=57)

  'paywith' => 

    array

      0 => string 'You dont have an email account for the requested payment.' (length=57)

ok

‘ok’ message means validate pass

Try with firebug… to see if there are some javascript errors…

Take a look at second parameter $clearErrors. Try setting it to false.

I had similar problem and found that solution is in that parameter.

What Yii does, if it is set to true (by default it is), it first clears all your custom added errors, and add errors during the validation process.




...

if($model->validate(null, false))

{

    die(var_dump($model->getErrors()));

}

...



One note:

You should put data validation in your model, not in a controller.

this one works!

yes, i have rules, but i need custom validation, is why im doing outside the model

thanks!

Its not a must follow approach, you can of course do that in a controller too, but if you’d like to follow best MVC practices, I suggest model :)

Cheers

Lifesaver :)