[solved] Unable to get Yii2 to show validation errors with HTML encode

[font="Courier New"]Hi,

I’m following this: yiiframework.com/doc-2.0/guide-start-forms.html

(No full links because the forum software says since I’m new I can’t post links)

Problem:

Works: localhost/~dev/hello/web/index.php?r=site/say-something

Doesn’t work: localhost/~dev/hello/web/index.php?r=site/entry , Gives blank output.

In controllers/SiteController.php:



    public function actionSaySomething()

    {

        $msg = "finally working on yii !";

        return $this->render('say-something', array('msg'=>$msg));

    }

   

    public function actionEntry() {

        $model = new EntryForm();

       

        $model->name = 'Random';

        $model->email = 'notanemail';

 

        if ($model->validate()) {

            $retVal = 'yay';

        } else {

            $retVal = $model->getErrors();

        }

        error_log(print_r($retVal, true));

 

        $this->render('entry', ['model'=>$model] );

    }

In view/say-something.php:



<?php

use yii\helpers\Html;

?>

<?=Html::encode($msg); ?>

In views/entry.php:



<?php

use yii\helpers\Html;

?>

<?php print_r($model->getErrors()); ?>

<p>You have entered the following information...</p>


<?php // exit; ?>


<ul>

    <li><label>Name</label>: <?= Html::encode($model->name); ?></li>

    <li><label>Email</label>: <?= Html::encode($model->email); ?></li>

</ul>


If I uncomment the "exit;" in views/entry.php I can see this:

Array ( [email] => Array ( [0] => Email is not a valid email address. ) )

You have entered the following information…

But if I try to display the error, I cannot, I get a blank output.

The error_log() logs the proper output($m to the apache log file.

This has to be a very basic thing I am missing.

Thanks in advance,

Dave

[/font]

Hi,

First I see following difference:

You are missing the “return” before the $this->render in your “actionEntry”. ;)

And I guess you are currelty checking out how things work together?

Because when you use ActiveForm (in future) it will display the validation errors automatically. ;)

Best regards

Hi,

Thank you for your quick reply (I tested a lot more things before replying again)

Copy-paste is not a strength of mine :D

The missed "return" means that the rendered content can be handled in the program … there is a renderPartial() that I intend to study later on.

Yes, very much so. Tried out several other types of basic displays in views.

Especially the conversion to JS checking code and client side validation is superb.

This will make HTML-form driven development much faster if I get the knack of the Yii2 process.

Thanks again.

Dave