2

I was trying to use the Yii2 ActiveForm encodeErrorSummary property because I wanted to put line-breaks on Yii2 validation error messages:

Example code snippet in MODEL file

 public function rules()
 {
   return [['username', 'required', 'message' => 'long message first line here<br> long message last line here']];
 }

Example code snippet in VIEW file

 $form = ActiveForm::begin(['id' => 'myform',
     'encodeErrorSummary' => false
  ]);
 ...
 echo $form->field($model, 'username');
 ...
 ActiveForm::end();

Official Yii2 Documentation describes encodeErrorSummary property as:

Whether to perform encoding on the error summary.

but it seemed not suitable for that in my case... Maybe it's me misunderstanding something (...error summary)?

So... what is it intended for, then?

Thank you!

Community
  • 1
  • 1
danicotra
  • 1,333
  • 2
  • 15
  • 34

1 Answers1

3

It seems like you need to configure the $fieldConfig property like this:

 ActiveForm::begin([
        'fieldConfig' => [
            'errorOptions' => ['encode' => false],
        ],
    ]);

for your requirement. The errorSummary is the summary that you echo with

<?= $form->errorSummary($model) ?>

before or after the form. What you want is a behavior at the field level, while this is an option to disable the encoding at the summary level.

danicotra
  • 1,333
  • 2
  • 15
  • 34
edoardo849
  • 103
  • 4
  • Wow, thank you for your prompt answer and for the Documentation references! Tested and accepted! :-D – danicotra Jan 04 '16 at 21:02
  • P.S. it seems a lots of Italians around here like or use Yii Framework... ( Sì, ho sbirciato il tuo profilo ;-) Ciao paisa', grazie ancora e arvëdse! - Ho mischiato un po' i dialetti così che gli anglofoni non pensino l'Italia sia solo tutta Nord o tutta Sud ;-D ) P.P.S. happy new year! – danicotra Jan 04 '16 at 21:12