1

I am developing a new login module in Yii framework:

class LoginForm extends Model
{
    public $username;
    public $password;
    private $_user = false;

    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
        ];
    }
}

Form validation is working for above code and error message is visible in input box below. But I need to display global error message in top of the page.

I need callback function after form validation. Please help me to fix this problem.

2
  • What did you try so far? What is your specific question/problem? Commented Aug 6, 2015 at 7:27
  • you can simply print the errors in your view file using getErrors() method Commented Aug 6, 2015 at 7:27

1 Answer 1

2

You can display the flash messages anywhere you want using the following snippet.

<?php
$flashMessages = Yii::app()->user->getFlashes();
if ($flashMessages) {
    echo '<ul class="flashes">';
    foreach($flashMessages as $key => $message) {
        echo '<li><div class="flash-' . $key . '">' . $message . "</div></li>\n";
    }
    echo '</ul>';
}
?>

The way you utilize the flash messaging is totally depends on how your app and layout files are structured.

You can do the same using javascript

<?php
Yii::app()->clientScript->registerScript(
   'myHideEffect',
   '$(".info").animate({opacity: 1.0}, 3000).fadeOut("slow");',
   CClientScript::POS_READY
);
?>

More info on this can be found here Yii Docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.