简体   繁体   English

在Yii2中进行表单验证后的回调Javascript函数

[英]Callback Javascript function after form validation in Yii2

I am developing a new login module in Yii framework: 我正在Yii框架中开发一个新的登录模块:

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. 利用Flash消息传递的方式完全取决于应用程序和布局文件的结构。

You can do the same using javascript 您可以使用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 可以在这里找到更多信息Yii Docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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