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.

Form Questions /

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

Passing arguments around a form (beginner question!)


Go to End


2 Posts   1384 Views

Avatar
cinoyter

Community Member, 1 Post

19 November 2014 at 6:16am

Edited: 19/11/2014 6:27am

Hello,

I need to build a page on a Silverstripe 3.1 site that does a search through an external database containing information on lots of documents. For various reasons, it is not possible to move this database into the SS database. I am trying to use a SS form to pass in the search terms, but I am having some trouble.

I have created a page for /community/library/ in the UI. This UI references a custom LibraryPage, LibraryPage_Controller, and template in LibraryPage.ss. I am able to successfully open /community/library and get a long readout of my library entries . The hard part is implementing the search.

- Following the "Forms" tutorial, I tried to implement a form handler like the "doBrowserPoll" function. However, I find that my function is only invoked with one argument, instead of two as depicted in the tutorial. I can't find any general documentation for form handlers. Am I missing something?

- Suppose I redirect from my form handler back to the main /community/library page. I would need to somehow get my arguments in, either using POST or GET arguments or else using the SS routing system. Any of the three is fine with me. Is it possible to "redirectBack" to another page and carry those arguments with me? If so, where will those arguments appear?

- As for the routing system: I can't seem to get my routes right. Just to test things, in mysite/_config/routes.yml I have defined a rule as such: 'community/library//$action/': 'LibraryPage_Controller' -- but this seems to do nothing. Is there a way to see all the routes which have been already defined? I can define an arbitrary route to a new section of the site, but it seems that if the route is set up in the UI, I can't then create sub-routes inside my routes.yml. (I am using the "templatable" plugin for multiple language translations, if it matters.)

Thanks for any help you can provide. I'm an experienced PHP developer and have used various MVC frameworks before, but I'm new to Silverstripe.

Cheers!

Avatar
martimiz

Forum Moderator, 1391 Posts

20 November 2014 at 3:45am

I'm not sure I can answer all your questions, but I'll make a start

I suppose you already found out about how to approch an external DataBase from whithin SilverStripe...

There isn't realy that much to say about Form handlers besides what you find in http://doc.silverstripe.org/framework/en/topics/forms: they are custom methods that are defined in the FormAction, and called automatically on Form submission. The paramaters $data and $form are resp. an array of all the submitted data and the Form class instance itself. You'd use the $data to query your database and render the page using a special template.

There is a somewhat rudimentary search functionality in the ContentControllerSearchExtension class combined with the SearchForm class that you could look at for ideas on how to set things up. (Note: ContentControllerSearchExtension is an extension class, a decorator for the Page_Controller, that uses $this->owner-> to refer to its controller instead of just $this.) You could easily set this up in your LibraryPage_Controller, without need for another controller.

From within the action handler, you can redirect if you want by either using $this->redirectBack() or $this->redirect($url, $code); You would do that on things like sending Email and returning results. Keeping state can be done by setting Session cookies http://api.silverstripe.org/3.1/class-Session.html (or writing things to DB, file, ...)

As to routing: If you set up your routes in yml (something like this):

---
Name: MyExternalSearch
After: cms/routes#coreroutes
---
Director:
	rules:
		'community/library//$action/': 'LibraryPage_Controller'

Then to have these changes take effect, you need to add them to the manifest by doing a mysite/?flush=all (?flush=1 might suffice in the newest release???)