0

I'm building a form and setting values via

$form = \Drupal::formBuilder()->getForm('My\Module', ['key1' => 'x']);

where I set the passed key1 as the default values for the associated fields : in My\Module\buildForm, you would find

public function buildForm(array $form, FormStateInterface $form_state, $presets = array()) {

    $form['key1'] = [
      '#type' => 'textfield',
      '#default_value' => isset($presets['key1']) ? $presets['key1'] : $query->get('key1')
    ];

    return $form;
}

This takes place in a controller not directly related to the form and I'd like to retrieve all values set, either as defaults or as parameters of the page, something like ['key1' => 'x'] if no parameter is passed to the page or ['key1' => 'v1'] if the page is called with ?key1=v1

How can I get all the values on my form array (besides filtering on the field names)?

1 Answer 1

1

Probably you put the preset values in the default values of form elements. They are at this point only in $form.

The values in $form_state are filled after the form is submitted, as a result of a GET/POST request.

But then your code, that calls the formBuilder, has no longer control. This will be handled by the submit method you have defined for the form.

4
  • FormState is mostly there to illustrate what I am looking for, I don't really need it. Correct me if I'm wrong, but you mean that there's no helper/method to extract the values of the form? Basically, I'm looking for a way to modify the form values in my controller and get the resulting values.
    – nikoshr
    Commented Sep 20, 2016 at 12:11
  • Can you put an example in your question, what is in $presets, where this is put in $form and what you want to extract?
    – 4uk4
    Commented Sep 20, 2016 at 12:14
  • Sure, I hope it's clearer
    – nikoshr
    Commented Sep 20, 2016 at 12:29
  • There is no helper method. #default_value is used to render the form in html. The processing of values only takes place after submit.
    – 4uk4
    Commented Sep 20, 2016 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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