How do I use loadDataFrom() to repopulate form with previously submitted values?

Silverstripe Version: 5.13

Question:

I’m sure there must be a relatively simple solution here, but I cannot seem to find it.

I have an encapsulated form where the user submits various values. After form submission, I would simply like to repopulate the fields with the previous values the user has input. It is not attached to a data object, I just want to reuse user’s values.

I have tried the following, without success:

$this->loadDataFrom($_REQUEST, Form::MERGE_IGNORE_FALSEISH);
$this->loadDataFrom(Form::getData(), Form::MERGE_IGNORE_FALSEISH);

Any tips much appreciated!

In your second example you’re calling getData() as a static method… Try replacing with $this→loadDataFrom($this→getData())

I think that should achieve what you’re after, but if you need to persist the submission longer (ie. the user may refresh the page, or navigate away and come back etc) you’d probably need to store in the session in the form handler. Something like:

// FORM
$sess = $this->getRequest()->getSession();
if ($data = $sess->get('MyFormData')) {
  $form->loadDataFrom($data);
}

// HANDLER
$sess = $this->getRequest()->getSession();
$sess->set('MyFormData', $data);
1 Like