Ok so here’s a rough explanation of what I’m doing. Building a registration form that has a privacy agreement before showing the form. It’s loaded first and then the form replaces the agreement (when the user accepts) through an Ajax request.
It works and is simple (a little code messy but simple - still playing with it) but I get one tiny little issue lol…
So for some reason the web app gets duplicated and replaces in the div through the ajax request.
Here’s the code, the registerPrivacy.php file is just html so that’s not affecting it. Without the ‘update’=>’…’ ajax won’t work and thus doesn’t pose the problem but then obviously my idea doesn’t work.
Remember - render() renders the view WITH the layout, renderPartial() renders it without the layout. On the ajax request, you should renderPartial() instead of render(), and you should only render what you want to get placed in #RegisterAgreement
I see well it’s the basic setup from CRUD statements just changed from create to register:
/**
* Guests Registers a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionRegister()
{
$model=new Users;
// Set the scenario for validation
$model->setScenario('register');
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
if($model->save())
{
Yii::app()->user->setFlash('register','Thank you for Registering with us. An admin will activate your account as soon as possible. Contact webmaster@sterlingsavvy.com for any issues.');
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('register',array(
'model'=>$model,
));
}
Replacing render with renderPartial in the register action doesn’t show anything but the view file (so no styling, no menus nothing). The initial call needs to be made as a render but from that point on I realize I have to use a render partial, so should I be putting in the logic that’s in the view through to the controller instead?
Fixed it! Thanks guys, the advice enlightened me a bit
New register action code:
/**
* Guests Registers a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionRegister()
{
$model=new Users;
// Set the scenario for validation
$model->setScenario('register');
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
if($model->save())
{
Yii::app()->user->setFlash('register','Thank you for Registering with us. An admin will activate your account as soon as possible. Contact webmaster@sterlingsavvy.com for any issues.');
$this->redirect(array('view','id'=>$model->id));
}
}
if((isset($_GET['continue'])) && ($_GET['continue']=='yes'))
{
$display = $this->renderPartial('_form', array('model'=>$model), true, false);
$this->renderPartial('register',array(
'model'=>$model,
'display'=>$display,
));
}
else
{
$display = $this->renderPartial('registerPrivacy', array('model'=>$model), true, false);
$this->render('register',array(
'model'=>$model,
'display'=>$display,
));
}
}
<h1>Registration Privacy Agreement</h1>
<p>All the privacy information will go here</p>
<br/>
<?php
echo CHtml::ajaxButton('I understand & agree', array('', 'continue'=>'yes'),array('type'=>'GET','update'=>'#RegisterAgreement'));
?>