0

I've got a regular silverstripe Form() method in my controller. It submits to a Submit() method as per the normal way of doing things.

On the page the form is in a tab and not displayed by default.

Is it possible to append an anchor to the URL the form submits to so that if validation fails, the form tab is displayed?

I have tried to do this by adding:

 $form->setFormAction($this->Link(). "/Submit#location-enquiry");

However the form does not seem to go to the Submit() method when doing that. Inspecting what's happening in chrome tools, it just posts to:

example/page/Submit  

..with the anchor appended

How can i append the anchor and still process the form?

munomono
  • 1,243
  • 9
  • 19
Will
  • 4,498
  • 2
  • 38
  • 65
  • does this one help? http://stackoverflow.com/questions/20560951/silverstripe-3-1-2-is-modifying-anchor-links/20562209#20562209 – munomono May 27 '14 at 12:09

1 Answers1

1

You can set if the form should be redirected back to the form name's hash on validation errors:

//false by default
$form->setRedirectToFormOnValidationError(true);

which will have the form's name appended in the redirect on errors:

return Director::redirect($pageURL . '#' . $this->FormName());

If the form does not have any errors, you could easily do the same in your processing method.

gherkins
  • 14,603
  • 6
  • 44
  • 70
  • Director::redirect() is deprecated in 3.0 and up. You should use $this->redirect($url) on a controller. – micmania1 May 27 '14 at 18:05
  • yep, or `Controller::curr()->redirect()` outside a controller. The line is just copied from the `Form` class, where it handles the redirect... – gherkins May 27 '14 at 19:27
  • So setting the setRedirectToFormOnValidationError to true redirects the form to #Form_Form and i can in this case adjust the front end code to use that. I'm not following the second part on how to change the redirect url? – Will May 29 '14 at 23:43