简体   繁体   中英

Callback Javascript function after form validation in Yii2

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM