class Html extends CHtml
{
public static function errorSummaryText($model,$header=null,$footer=null)
{
$content='';
if(!is_array($model))
$model=array($model);
foreach($model as $m)
{
foreach($m->getErrors() as $errors)
{
foreach($errors as $error)
{
if($error!='')
$content.="$error\n";
}
}
}
if($content!=='')
{
if($header===null)
$header=Yii::t('app','Please fix the following input errors:')."\n";
return $header."\n$content\n".$footer;
}
else
return '';
}
}
Jerry,
Can you explain in which way your function is better than original CHtml::errorSummary one? I found that you only removed $htmlOptions support and you are returning pure contents where original function returns them nicely formatted inside div tag.
Both changes are making your function less useful than original one, but I might be missing something.
Looking at the topic, it returns plain text for errors.
I think that the goal was to create something confortable for log or something like that.
Thanks! But still… wouldn’t it be easier to simply strip_tags on what original CHtml::errorSummary() returns, than to employ (write) own class for this purpose?
Yes this looks like it’s for logs. This works excellent for what I needed to log errors in my cron jobs. I think we can make a more generalized version of this though which handles all similar situations. Here is my first attempt (based on more current code) which I believe should be compatible with the current calls:
public static function errorSummary($model, $header=null, $footer=null, $htmlOptions=array()) {
$content='';
if(!is_array($model)) $model=array($model);
if(isset($htmlOptions['itemFormat'])) {
$itemFormat=$htmlOptions['itemFormat'];
unset($htmlOptions['itemFormat']);
}
else $itemFormat="<li>%s</li>\n";
if(isset($htmlOptions['firstError'])) {
$firstError=$htmlOptions['firstError'];
unset($htmlOptions['firstError']);
}
else $firstError=false;
foreach($model as $m) {
$itemErrors=$m->getErrors();
foreach($itemErrors as $errors) {
foreach($errors as $error) {
if($error!='') $content.=sprintf($itemFormat, $error);
if($firstError) break;
}
}
}
if($content!=='') {
if(isset($htmlOptions['containerFormat'])) {
$containerFormat=$htmlOptions['containerFormat'];
unset($htmlOptions['containerFormat']);
}
else $containerFormat="<p>%s</p>\n<ul>\n%s</ul>%s";
if($header===null) $header=Yii::t('yii', 'Please fix the following input errors:');
if(!isset($htmlOptions['class'])) $htmlOptions['class']=self::$errorSummaryCss;
return self::tag('div', $htmlOptions, sprintf($containerFormat, $header, $content, $footer));
}
else return '';
}
If using sprintf to insert the content of the messages is, for some reason, not fast enough you could improve performance by getting rid of $containerFormat and just adding that formatting to the end of $header and start of $footer but that would break some current stuff most likely.