如何以红色显示表单errorSummary()?

我有这样的形式:

<?php 
   $form = $this->beginWidget('CActiveForm', array(
            'id' => 'login-form',
            'enableClientValidation' => true,
            'clientOptions' => array(
                'validateOnSubmit' => true,
            ),
        ));


    $errorMsg = $form->errorSummary($userModel, "", "");

    if(!empty($errorMsg))
    {
        echo '<div class="alert alert-danger" style="text-align:center; width:800px; margin:auto;">' . $errorMsg . '</div>';
         echo '<br>';
     }

    $form->labelEx($userModel, 'email');
    echo $form->textField($userModel, 'email');
    $form->error($userModel, 'email');
?>

我试图在电子邮件无效时打印错误消息,但我想使用bootstrap中的红色背景打印它。 问题是没有任何错误,它仍然显示红色条。

似乎发生这种情况是因为errorSummary()永远不会为null


输出: 在此处输入图像描述

采纳答案:

这不是摘要的工作原理。

errorSummary()函数始终返回要添加到页面的内容。
该函数确保在没有错误的情况下将返回的div标记为隐藏。

这样做的原因是,总结也用于前端验证。 javascript将在需要时更新并隐藏/显示div

所以基本上你应该这样做:

echo $form->errorSummary($userModel, "", "", array('class' => "alert alert-danger"));

并摆脱额外的if(!empty($errorMsg)) ...

author: blizz

参考更多解答: How to show form errorSummary() in red? ,转载请保留出处如何以红色显示表单errorSummary()?及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:html