ajax CActiveForm的逻辑流究竟是什么?(yii框架)


What exactly is the flow of logic for an ajax CActiveForm? (yii framework)

我很难准确理解经过ajax处理的CActiveForm的逻辑流。

上下文:

我使用ajax进行登录和注册。登录有效,并在成功登录后将用户重定向到主页。注册有效,但注册成功后不会将用户重定向到主页。它就在那里。当然这有点奏效,但我不明白它到底在幕后做什么。

控制器代码:(控制器/型号/视图代码在登录和注册操作之间几乎完全相同)

public function actionLogin()
{
    $model = new LoginForm;
    // if it is ajax validation request
    if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
        echo CActiveForm::validate($model);
        app()->end();
    }
    // render and nonajax handling here...
}
public function actionRegister() {
    $model = new RegisterForm;
    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax'] === 'register-form')
    {
        echo CActiveForm::validate($model);
        app()->end();
    }
    // render and nonajax handling here...
}

问题:

我尝试过进行打印行调试,以查看代码的最终去向,但对于ajax请求,代码似乎正好在app()->end();处结束。为什么登录操作重定向我而不是注册操作?指定重定向位置的代码在哪里?我知道对于app()->end();代码,yii除了退出应用程序外,还会引发"onEndRequest"事件。重定向的代码在这里吗?我在受保护的文件夹中查找了包括文本"onEndRequest"的任何内容,但找不到任何存在的迹象。

编辑:

我也想在注册后发送一封电子邮件,但如果不正确理解这个精简的CActiveForm的逻辑流程,我就无法发送电子邮件。我该把代码放在哪里?(我知道如何编码,只是不知道把它放在哪里)

TL;DR;但我的答案几乎包含了引用的句子和现有的代码,实际上它并不太长:)

我使用ajax进行登录和注册。登录工作和重定向用户在成功登录后进入主页。注册作品,但成功后不会将用户重定向到主页登记它就在那里。当然有点管用,但我不行了解它在幕后到底在做什么。

当你进行验证和提交表单时,它们的工作方式是一样的,但你的应用程序是否重定向取决于你下一步要做什么,我看到的是你的评论行`

//在此处进行渲染和非jax处理…`

我提出了登录代码的下一部分(Yii默认),我认为你已经有了这个代码。

// collect user input data
        if(isset($_POST['LoginForm']))
        {
            $model->attributes=$_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }

假设你看到了完成任务的重定向行,我不会再谈论returnUrl了。

一旦你设置了你的表单有'enableAjaxValidation'=>true, Yii将不得不绑定看起来像的函数

jQuery('#your-form-id').yiiactiveform( ...);

这是通过ajax验证表单的原因,这意味着它将到达控制器上的两行以下

        echo CActiveForm::validate($model);
        app()->end(); //end request and return ajax result

如果你在没有yii-ajax验证的情况下提交表格,你永远不会通过下面的检查达到上面的行

if(isset($_POST['ajax']) && $_POST['ajax'] === 'your-form')

如果没有返回错误,那么一旦没有回调函数,函数yiiativeform接下来会做什么?让我们打开下面的源代码来检查

'framework'web'js'source'jquery.yiiactiveform.js

我刚刚引用了一个必要的零件

if (settings.afterValidate === undefined || settings.afterValidate($form, data, hasError)) {
                                if (!hasError) {
                                    validated = true;
                                    var $button = $form.data('submitObject') || $form.find(':submit:first');
                                    // TODO: if the submission is caused by "change" event, it will not work
                                    if ($button.length) {
                                        $button.click();
                                    } else {  // no submit button in the form
                                        $form.submit();
                                    }
                                    return;
                                }
                            }

如果表单通过了验证,它只需找到提交按钮并单击即可,使表单以正常方式提交。在您的情况下,这意味着表单将再次发布到actionRegister,在那里它将不再满足ajax验证

public function actionRegister() {
    $model = new RegisterForm; 
    //where perform ajax validation but form which was submitted normally would not reach
      .....
//the next part what you need would come
if(isset($_POST['RegisterForm']))
    {
        $model->attributes=$_POST['RegisterForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate()){
                    //send mail here ... then redirect
            $this->redirect(Yii::app()->user->returnUrl);
             }
    }
$this->render('register',array('model'=>$model));
}