php - What exactly is the flow of logic for an ajax CActiveForm? (yii framework) -
i having trouble understanding flow of logic ajax processed cactiveform.
context:
i using ajax login , registration. login works , redirects user homepage after successful login. register works not redirect user homepage after successful registration. sits there. sure kinda works, don't understand doing behind scenes.
controller code: (the controller/model/view code same between login , register actions)
public function actionlogin() { $model = new loginform; // if ajax validation request if (isset($_post['ajax']) && $_post['ajax'] === 'login-form') { echo cactiveform::validate($model); app()->end(); } // render , nonajax handling here... } public function actionregister() { $model = new registerform; // if ajax validation request if(isset($_post['ajax']) && $_post['ajax'] === 'register-form') { echo cactiveform::validate($model); app()->end(); } // render , nonajax handling here... }
question:
i have tried doing print line debugging see code ends going, seems ajax requests, code ends right @ app()->end();
. why login action redirecting me , not register action? code specifies redirected? know app()->end();
code, yii raise "onendrequest" event in addition exiting app. code redirect is? have looked includes text "onendrequest" in protected folder can't find indication of existence.
edit:
i want send email out after registration, can't without proper understanding of flow of logic ajaxified cactiveform. place code? (i know how code up, don't know put it)
tl;dr; answer contains quoted sentenses , existed codes, not long:)
i using ajax login , registration. login works , redirects user homepage after successful login. register works not redirect user homepage after successful registration. sits there. sure kinda works, don't understand doing behind scenes.
they work same ways when did validation , submit form, whether or not app redirect or not depend on next, 1 looking comment line `
// render , nonajax handling here...`
i bring next part of login code (yii default) 1 thought have had on side.
// collect user input data if(isset($_post['loginform'])) { $model->attributes=$_post['loginform']; // validate user input , redirect previous page if valid if($model->validate() && $model->login()) $this->redirect(yii::app()->user->returnurl); }
you suppose see redirect line did job , won't talk returnurl anymore.
once has set form have 'enableajaxvalidation'=>true,
yii have bind function looked like
jquery('#your-form-id').yiiactiveform( ...);
this use validating form through ajax of cause, means reach below 2 lines on controller
echo cactiveform::validate($model); app()->end(); //end request , return ajax result
if submit form without yii ajax validation, never reach above lines inspection below
if(isset($_post['ajax']) && $_post['ajax'] === 'your-form')
if no error returned, function yiiactiveform next once there no callback function? let's open source below inspect it
\framework\web\js\source\jquery.yiiactiveform.js
i quoted 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 submission caused "change" event, not work if ($button.length) { $button.click(); } else { // no submit button in form $form.submit(); } return; } }
if form passed validation, find submit button , click, make form submit way. in case, means form post actionregister
again, not meet ajax validation anymore
public function actionregister() { $model = new registerform; //where perform ajax validation form submitted not reach ..... //the next part need come if(isset($_post['registerform'])) { $model->attributes=$_post['registerform']; // validate user input , redirect previous page if valid if($model->validate()){ //send mail here ... redirect $this->redirect(yii::app()->user->returnurl); } } $this->render('register',array('model'=>$model)); }
Comments
Post a Comment