21278 Posts in 5728 Topics by 2599 members
|Go to End|
16 March 2012 at 5:16am
I have a site with a custom search form as an include. The form itself works fine and posts data to the controller. The issue is that when we land on the results page, the search criteria entered into the form is lost and the form fields are left blank/unchecked.
Does anyone know of a way to have the form show the data that was entered once submitted.
16 June 2012 at 3:45am
I am trying to do a similar thing. Did you ever find a solution?
16 June 2012 at 4:09am
Hope that helps,
16 June 2012 at 4:36am Last edited: 19 June 2012 2:38am
Thanks for the response. What method did you use to persist the values? Did you just write out the script with the values as part of the page?
I am looking at two options right now:
1. Persist the data using $form->SaveInto, saving the data object to session, then re-loading using $form->loadDataFrom. It make intuiative sense, but I'm not sure if it will work.
2. Forget Silverstripe form persistence and use AJAX to render my search results without reloading the page.
Do you have any sample code that would accomplish #1 above? I'd really like to know because it is one of those things that is valuable if you are doing any sort of app development style features in your site.
16 June 2012 at 5:19am
I am making some progress using method #1 above. The code works below, except for CheckBoxSetField and I suspect that is because it is a has_many relationship.
public function SearchForm()
$fields = new FieldSet(
new DropdownField('Country','Country', array('NZ' => 'New Zealand','US' => 'United States', 'GEM'=> 'Germany')),
new CheckboxSetField("System Type", "System Type", array("Board" => "Board","Enclosed" => "Enclosed"))
$actions = new FieldSet(
new FormAction("doResults", "Search")
$form = new Form($this, "SearchForm", $fields, $actions);
$_SESSION['searchFormData'] = null;
public function doResults($data, $form)
$searchResults = DataObject::get("ProductPage");
$formData = new DataObject();
$_SESSION['searchFormData'] = $formData;
return array("SearchResults" => $searchResults);
17 June 2012 at 1:24am Last edited: 17 June 2012 1:25am
First of all - being a moderator doesn't necessarily make one a guru - it's more about responding to spamming and other unwanted behaviours
Aside from that, I think that, if you render the page results right away from the doResults method, the posted data should still be available in $_REQUEST or $_POST or $data or what have you, so you could just add while creating the search fields. At least that's how I do it. But your situation might be more complex?
19 June 2012 at 2:41am Last edited: 19 June 2012 2:42am
Indeed, I modified the above post ;-)
I knew that was a possibility and I was hoping there was something in the Silverstripe Core that would automate this a bit, since that exists in other frameworks that I have worked with. And it looks like it does, with the SaveInto and LoadDataFrom functions. I've replaced my CheckboxSetField with standard Checkboxes and the solution I came up with is working well for me.
|Go to Top|