2

I have this form:

<?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');
?>

I am trying to print the error message when the email is invalid, but I want to print it using the red background from bootstrap. The problem is that without any errors it still shows the red bar.

It seems that this happens because errorSummary() is never really null


output: enter image description here

J_Strauton
  • 2,270
  • 3
  • 28
  • 70

2 Answers2

4

Thats not how things work with the summary.

The errorSummary()-function always returns content to be added to the page.
The function makes sure the returned div is marked as hidden if there are no errors.

The reason it is done like that is so that the summary is there for front-end validation as well. The javascript will update and hide/show the div when needed.

So basically you should do this:

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

And get rid of the extra if(!empty($errorMsg)) ...

Blizz
  • 8,082
  • 2
  • 33
  • 53
  • Yup, that works. I just found it here: http://stackoverflow.com/questions/20718652/how-can-i-add-class-to-errorsummay-without-remove-header-message – J_Strauton Jul 08 '15 at 07:12
0

You can change class like this:

<?= $form->errorSummary($model, ['class' => 'alert alert-danger']); ?>