0

I am having trouble understanding exactly what the flow of logic is for an ajax processed CActiveForm.

Context:

I am using ajax for login and registration. Login works and redirects the user to the homepage after successful login. Register works but DOES NOT redirect the user to the homepage after a successful registration. It just sits there. Sure it kinda works, but I don't understand what exactly it is doing behind the scenes.

Controller Code: (the controller/model/view code are almost exactly the same between the login and register actions)

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...
}

Question:

I have tried doing print line debugging to see where the code ends up going, but it seems that for ajax requests, the code ends right at app()->end();. Why is the login action redirecting me and not the register action? Where is the code that specifies where I will be redirected? I know that for the app()->end(); code, yii will raise a "onEndRequest" event in addition to exiting the app. Is this where the code for the redirect is? I have looked for anything that includes the text "onEndRequest" in my protected folder but can't find any indication of its existence.

EDIT:

I also want to send an email out after registration, but can't do so without a proper understanding of the flow of logic with this ajaxified CActiveForm. Where would I place that code? (I know how to code it up, I just don't know where to put it)

train
  • 610
  • 2
  • 11
  • 20
  • To answer your first question, why there's no redirect on registration, you should show us your full `actionRegister()` code. – Michael Härtl Aug 20 '13 at 05:56

1 Answers1

1

TL;DR; but almost my answer contains the quoted sentenses and existed codes, actually it was not too long:)

I am using ajax for login and registration. Login works and redirects the user to the homepage after successful login. Register works but DOES NOT redirect the user to the homepage after a successful registration. It just sits there. Sure it kinda works, but I don't understand what exactly it is doing behind the scenes.

They work same ways when you did validation and submit form, but whether or not your app redirect or not depend on what you would do next, which one I was looking is your comment line `

// render and nonajax handling here...`

I bring up the next part of login code (Yii default) which one I thought you have already had on your side.

// 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);
        }

You are suppose to see the redirect line which did it job and I won't talk much about returnUrl anymore.

Once you has set your form have 'enableAjaxValidation'=>true, Yii would have to bind function that looked like

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

This use for validating your form through ajax of cause, it means it would reach below two lines on your controller

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

If you submit form without yii ajax validation, you never reach above lines by the inspection below

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

if no error returned, what would the function yiiactiveform do next once there are no callback function? Let's open the source below to inspect it

\framework\web\js\source\jquery.yiiactiveform.js

I just quoted a necessary part

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;
                                }
                            }

If the form passed the validation, it simply find the submit button and do click, make the form submit by normally way. In your case, it means the form would post to the actionRegister again, where it would not meet the ajax validation anymore

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));
}
Community
  • 1
  • 1
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39
  • This was what I thought at first too, but I tried print-line debugging with firephp and if I submit by ajax, none of the code below "app()->end();" ever gets executed...I appreciate the help and I guess I will mark this as the accepted answer, but question still stands. I will try to look further into this again... – train Aug 21 '13 at 01:30