[英]Yii Custom error field
hello guys I am trying to create a custom error field for all of my models where in this field will contain all of the errors what ever the current controller/action is.. like for example in my main.php: 大家好,我正在尝试为我的所有模型创建一个自定义错误字段,其中该字段将包含当前控制器/操作所发生的所有错误..例如在我的main.php中:
<header>
...
</header>
<div id="container">
<div id="my_custom_error_field">
<?php //all models error goes in here ?>
</div>
<div id="dynamic_content">
<?php echo $content; ?>
</div>
</div>
<footer>
...
</footer>
my question is how will I do this? 我的问题是我该怎么做? is there a built in method that gets the current models errors??
是否有获取当前模型错误的内置方法? should I create a widget for this?
我应该为此创建一个小部件吗?
Thanks in advance.. 提前致谢..
If you are using CActiveForm, you can use $form->errorSummary($model);
如果使用的是CActiveForm,则可以使用
$form->errorSummary($model);
: :
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'my-form',
'enableClientValidation'=>false,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<div id="my_custom_error_field">
<?= $form->errorSummary($model); ?>
</div>
# The rest of the form goes here...
<?php echo CHtml::submitButton('Submit', array('class'=>'btn btn-primary')); ?>
<?php $this->endWidget(); ?>
If you want all the errors for a model, use $model->getErrors();
如果要获取模型的所有错误,请使用
$model->getErrors();
<div id="my_custom_error_field">
<?php
$errors = $model->getErrors();
foreach($errors as $key => $error) {
// echo out to page
}
?>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.