Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to make getNextStep() work in MultiForm?


Go to End


6 Posts   3017 Views

Avatar
SilverRay

Community Member, 167 Posts

8 August 2009 at 4:13am

Why oh why does the form always go to the 'PersonalDetailsFormStep', no matter what the choice of 'AreaChoice' is? See the following for code:

http://pastie.org/575633

I don't see it...

Thanks!

Avatar
SilverRay

Community Member, 167 Posts

8 August 2009 at 11:21pm

Anyone? The multiform module is excellent (and overlooked as I can not find many posts about it in the forum) except for this little problem I have...

Thanks.

Avatar
Willr

Forum Moderator, 5523 Posts

9 August 2009 at 11:42am

Does it call the getNextStep() method at all? a) its not calling that or b) $this->loadData() is not returning anything.

Avatar
SilverRay

Community Member, 167 Posts

9 August 2009 at 9:06pm

Thanks for your reply, willr.

AFAIK, it seems getNextStep() gets called. Also, the value of AreaChoice is written in the session variable. I very quickly checked loadData() with an echo and that seems OK as well. The thing is, weird things are happening in the if-else statement. It goes to whatever step I define after the 'else' statement...

Avatar
Sean

Forum Moderator, 922 Posts

10 August 2009 at 10:01am

Edited: 10/08/2009 10:22am

I believe your problem is because "AreaChoice" is not available until the step is saved. MultiForm::next() will check for what the next step is, before saving the current step's data.

Also, you should probably do your getNextStep() check by checking for AreaCode as an array index, because it's currently a pointer to a property on $this->loadData() e.g.:

public function getNextStep() {
	$data = $this->loadData();
	if($data['Gender'] == 'Male') {
		return 'TestThirdCase1Step';
	} else {
		return 'TestThirdCase2Step';
	}
}

If we have a look at MultiForm.php, specifically the next() method contained in that file:

public function next($data, $form) {

	// Get the next step class
	$nextStepClass = $this->getCurrentStep()->getNextStep();
		
	if(!$nextStepClass) {
		Director::redirectBack();
		return false;
	}

	// custom validation (use MultiFormStep->getValidator() for built-in functionality)
	if(!$this->getCurrentStep()->validateStep($data, $form)) {
		Director::redirectBack();
		return false;
	}
		
	// Save the form data for the current step
	$this->save($form->getData());

	...

I think it's perfectly acceptable to assume you can use the current step data to determine where to go next. The problem is the above code doesn't do that. Ideally, the above code would call $this->save() first, before checking $this->getCurrentStep()->getNextStep()

You could have a go at fixing this by copying the next() method from MultiForm into your subclass for the form you're making, this will override the default behaviour when someone clicks the Next button in the form.

In the mean time, I'll open up a ticket at http://open.silverstripe.com to get this fixed for a future multiform release.

Hope this helps!

Cheers,
Sean

Avatar
SilverRay

Community Member, 167 Posts

12 August 2009 at 8:37pm

Sean, thanks for your reply! I'll give it a try again, with the suggestions you provided.

Thanks.