Yii Framework Forum: Multi Page Form With Validation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Multi Page Form With Validation Rate Topic: ***** 1 Votes

#1 User is offline   Dean Sanderson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 18-July 12

Posted 06 March 2013 - 07:15 AM

Hi i am having issues trying to create a multipage form. I have read lots of info on this especially this
yii-multi-page-form-wizard-best-practice on stackoverflow

But I am having issues with validation. When trying to move from step one of the form, validation fails because its trying to validate fields that are not in that step.
I then removed validation from each step and instead validated on save. But after doing this i notices none of the data was being stored. So when the validation fails and goes back to the first page the form is empty.

Could anyone see what i am doing wrong?

My controller code is below:

public function actionCreate()
	{
		$model=new Accounts;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['accountsStep2']))
		{
			$this->setPageState('step1',$_POST['Accounts']);
			$model=new Accounts('step1');
			$model->attributes = $_POST['Accounts'];
	
				$this->render('create',array(
					'model'=>$model,
					'formLevel' => '2',
				));

		}
		elseif(isset($_POST['AccountsFinish']))
		{
			$model->attributes=$_POST['Accounts'];
			if($model->save()) {
				$this->redirect(array('view','id'=>$model->accountID));
			} else {
				$this->render('create',array(
				'model'=>$model,
				'formLevel' => '1',
			));	
			}
			
		} else {

			$this->render('create',array(
				'model'=>$model,
				'formLevel' => '1',
			));
		}
	}


0

#2 User is offline   Dean Sanderson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 18-July 12

Posted 07 March 2013 - 04:06 AM

All Sorted, Seems i wasnt doing setting the second form to be stateful!

My only other issue is how do i part validate the form. So i can validate fields as we go along.
0

#3 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 07 March 2013 - 04:59 AM

I think you can achieve your goal with scenarios.

Define validation rules for each step (=>scenario) and use them to validate each step.
0

#4 User is offline   Ivo Pereira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 29-July 12

Posted 08 March 2013 - 05:00 AM

I'm building a multi-step registration form. I have scenarios for each step (step1,step2,etc).

In the controller, I check if the model has any errors, always validating using the previous step:
if($_GET['step'])>1) {
   $step-= 1;
} else {
   $step= 1;
}


if ( $_POST ) {
   $userFormModel = new UserForm('step'.$step);
   $userFormModel->attributes = $POST;
   
   if ( $userFormModel->validate() {
       $step = $_GET['step'];
   } else {
       $errors = $userFormModel->getErrors();
   }

}

$this->render( 'step'.$step,
               array('errors',$errors)
);



This is what I came with, from my head right now. Not tested. But I think you got the idea. You'll be always checking the previous step when calling the controller.
0

#5 User is offline   Dean Sanderson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 18-July 12

Posted 08 March 2013 - 12:35 PM

perfect just what i am looking for cheers!
0

#6 User is offline   Ivo Pereira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 29-July 12

Posted 08 March 2013 - 12:49 PM

No problem. Did you understand all the logic? I know it is a bit tricky, but a little more flexible than using a lot of information.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users