21278 Posts in 5728 Topics by 2599 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 374 Views |
-
Persisting form data on custom forms

16 March 2012 at 5:16am
Hi,
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.
Cheers,
T.
-
Re: Persisting form data on custom forms

16 June 2012 at 3:45am
Hi,
I am trying to do a similar thing. Did you ever find a solution?
Thanks,
J. -
Re: Persisting form data on custom forms

16 June 2012 at 4:09am
Jon,
We ended up using javascript to re-set the values once the page loaded. Not the prettiest solution but it works for what we need.
Hope that helps,
T.
-
Re: Persisting form data on custom forms

16 June 2012 at 4:36am Last edited: 19 June 2012 2:38am
Hi Tim,
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?
Dear
MODERATORSGURUs: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.
Thanks,
Jon -
Re: Persisting form data on custom forms

16 June 2012 at 5:19am
Hi,
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 TextField("TestTextField"),
new CheckboxField("checkboxfieldtest"),
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);
if (isset($_SESSION['searchFormData']))
{
$form->loadDataFrom($_SESSION['searchFormData']);
$_SESSION['searchFormData'] = null;
}
return $form;
}public function doResults($data, $form)
{
$searchResults = DataObject::get("ProductPage");
$formData = new DataObject();
$form->SaveInto($formData);
Debug::dump($formData);
$_SESSION['searchFormData'] = $formData;
return array("SearchResults" => $searchResults);
} -
Re: Persisting form data on custom forms

17 June 2012 at 1:24am Last edited: 17 June 2012 1:25am
Hi there,
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?
-
Re: Persisting form data on custom forms

19 June 2012 at 2:41am Last edited: 19 June 2012 2:42am
Hi Martimiz,
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.
Thanks.
Jon
| 374 Views | ||
|
Page:
1
|
Go to Top |


