Hello,
I have been working on a form that presents a confirmation screen before saving the data. In this screen the user should be able to go back and modify or confirm and submit (save to database) the fields.
One approach was using just a js confirmation message but this is not what I needed. What I did is redirecting the user to another view the first time the form is submitted. This view creates a form with invisible fields and also displays the input as text. Something like this:
<div class="row">
<b> <?php echo CHtml::activeLabel($model,'first_name'); ?></b>
<?php echo CHtml::activeHiddenField($model,'first_name'); ?>
<?= $model->first_name ?>
</div>
To differentiate between the first submission of the form and the submission of the confirmation screen form I added in the confirmation screen another hidden field named ‘conf’
<?php echo CHtml::hiddenField('conf','1'); ?>
The controller looks for this ‘conf’ field and if present it saves the data. To be sure that the data is still valid it calls the verify() method. The problem is that the form includes a captcha so by the time the confirmation form is submitted yii says that the captcha code is not valid anymore. The way I solved this is using scenarios:
public function actionRegister()
{
$form=new User();
// collect user input data
if(isset($_POST['User']))
{
$conf = false;
if(isset($_POST['conf']) && $_POST['conf'] == 1) {
$form=new User('conf'); // create user with 'conf' scenario so that the captcha code is not needed
$conf = true;
}
$form->attributes=$_POST['User'];
if($form->validate())
{
if ($conf === true) {
$form->save();
$this->redirect($this->render('success')); // the success screen after the form is saved to db
}
$this->redirect($this->render('confscreen',array('model'=>$form))); // the confirmation form with hidden fields
}
}
$this->render('myform',array('model'=>$form)); // display the registration form
}
I can see one flaw in my design (the captcha code can be bypassed if the form is submitted directly from the conffirmation form screen). I would like to know of there is a better approach, but until I know a better solution I leave it like this.
The form works. The only thing is that I do not have a button to go back from confirmation screen to the initial form so that the user can change the data instead of submitting. The only way I know how to do this is using a javascript go back function sent to the browser. I am very sure that this is not a good solution (the captcha will probably be loaded from the browsers cache but when submitted it is going to be marked as not valid). ALso, I am sure there is a very easy solution on how to implement the go back button. Any ideas?
Edit: Now that I wrote this message I noticed that even though I am rendering different views using $this->redirect, the same initial controller action is called in all of them. This makes the confirmation screen inaccessible unless called from the original form so it is not possible to submit the form without filling out the captcha, right?