Jump to:

23493 Posts in 19013 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Persisting form data on custom forms

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 824 Views
  • NETim
    Avatar
    Community Member
    24 Posts

    Persisting form data on custom forms Link to this post

    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.

  • JonYYC
    Avatar
    Community Member
    10 Posts

    Re: Persisting form data on custom forms Link to this post

    Hi,

    I am trying to do a similar thing. Did you ever find a solution?

    Thanks,
    J.

  • NETim
    Avatar
    Community Member
    24 Posts

    Re: Persisting form data on custom forms Link to this post

    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.

  • JonYYC
    Avatar
    Community Member
    10 Posts

    Re: Persisting form data on custom forms Link to this post

    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 MODERATORS GURUs:

    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

  • JonYYC
    Avatar
    Community Member
    10 Posts

    Re: Persisting form data on custom forms Link to this post

    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);
    }

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Persisting form data on custom forms Link to this post

    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?

  • JonYYC
    Avatar
    Community Member
    10 Posts

    Re: Persisting form data on custom forms Link to this post

    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

    824 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.