Hi every one, is there a way to put a test in the view that contain an CActiveForm ?
i mean is there a way to something like that :
if($form->hasError)
{
echo $form->errorSummary($model);
}
i know that “hasError” is not a property in hasError, i just put it like example
mdomba
(Maurizio Domba Cerin)
June 15, 2011, 10:27am
2
Why would you need this…
if you check the source of errorSummary()… you will see that it does not output anything if there are no errors…
mdomba
(Maurizio Domba Cerin)
June 15, 2011, 10:32am
3
ops… the CHtml::errorSummary() does not echo anything if there are no errors…
but
the CActiveForm::errorSummary() does echo a placeholder even if there are no errors…
To test if there are errors… you can use $model->getErrors() - http://www.yiiframework.com/doc/api/1.1/CModel#getErrors-detail
mdomba:
Why would you need this…
i need that because i want to display error message in a specefic box that contain other tags
<div class="error ">
<a class="close" href="#"><img alt="close" title="Close this notification" src="<?php echo Yii::app()->request->baseUrl; ?>/themes/img/cross_grey_small.png"></a>
<div>
<?php echo $form->errorSummary($model); ?>
</div>
</div>
and i saw that when i want to change the error summary, i can only change the css, but not the content of the error summary, so i thought that putting this specefic error message into a test and become like i suppose :
<?php if($form->hasError) : ?>
<div class="error ">
<a class="close" href="#"><img alt="close" title="Close this notification" src="<?php echo Yii::app()->request->baseUrl; ?>/themes/img/cross_grey_small.png"></a>
<div>
<?php echo $form->errorSummary($model); ?>
</div>
</div>
<?php endif; ?>
if you have an other way to display this i’ll be happy to hear it ^^
Yes exactly ! that’s what i’m searching thanx !
but to develop this topic, i want to know how can i costomise the errorsummary by adding html tags ? (like the example that i give)
mdomba
(Maurizio Domba Cerin)
June 15, 2011, 11:06am
6
errorSummary() has it’s fixed display…
but you can use $model->getErrors() to get the errors list… and display it anyway you want…
Noodled my brain over this for an hour. I have a specific class I need to attach to the error messages. So made the process a bit stickier since empty error messages would be rendered as an empty div with a class attached. I was focused on the CActiveForm functions. Thanks mdomba!
What I got that works:
<? if(array_key_exists('username', $model->getErrors())): ?>
<?= $form->error($model,'username',array('class'=>'message errormsg')); ?>
<? endif; ?>
<? if(array_key_exists('password', $model->getErrors())): ?>
<?= $form->error($model,'password',array('class'=>'message errormsg')); ?>
<? endif; ?>