0

I am creating a form with "add more" functionality. Form builder retrieves values according to form built. keys are generated using uuid service. I also have to save them. So they are saved into form storage.

$form_state->get('links', $links)
...
$form_state->get('links')

On the next ajax submission storage is empty?! Also I use a block configuration form. So it uses sub-forms. May be the problem is related with. This is related problem. How to prevent SubFormState storage leaking into parent?

  • I see this that it is a problem with SubFormState. I met similar problems. This is the current solution: $values = $form_state->getCompleteFormState()->get('links') I hope somebody will give me an advise how to work with SubForm states correctly. Data population does not work properly. – viktor.likin Jul 7 '18 at 22:33
  • I've found a mention that the form state storage is not shareable between requests, there is another solution drupal.stackexchange.com/a/93747/27126 – viktor.likin Jul 7 '18 at 23:05
0

You say this is about building the form. If you display a form on a page, then this form is rendered, but the form object is not stored yet. This happens later when the form is submitted, then you can use $form_state.

Generally when you build a form this is the same like rendering of non form content. Put the initial values in HTML (in the form elements). Similar to default values for visible fields use hidden fields for other values. You also have to add the correct cache metadata to the form so that the render cache varies by the data and is invalidated when the data is modified.

If you don't want to transport data in a rendered form then use a session or tempstore. This kind of storage is not restricted to form requests, so strictly not connected to this issue, but for logged in users these are easily available and for anonymous you can start a session, which you need to do anyway if you want to personalize a form.

  • I see that the form storage works differently with D7. Now I use as you have mentioned above PrivateTempStoreFactory. Is a formId good case for storage key? – viktor.likin Jul 8 '18 at 11:38
  • Possibly, if there is only one instance of the form for each user. But without knowing which kind of data this is it's difficult to recommend how to store it. – 4k4 Jul 8 '18 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.