On an submission handler, should I fetch the user input via \Drupal::request()->request->get()
or is $form_state->getValues();
the better (more saver) way?
2 Answers
$request->request->all() != $form_state->getValues()
$request->request->all() === $_POST
$request->query->all() === $_GET
See in Drupal\Core\Form\FormBuilder::buildForm()
$input = $form_state->isMethodType('get') ? $request->query->all() : $request->request->all();
$form_state->setUserInput($input);
$form_state->getValues() is already modified data that is formed on the basis of $form_state['input'] and #default_value
See in Drupal\Core\Form\FormBuilder::handleInputElement();
Pay attention to #value_callback
it forms the value of the field
-
1Thanks Mykola! Yep, I missed to check the code in:
Drupal\Core\Form\FormBuilder::buildForm()
. Now I got it, thx!– CalarCommented Apr 12, 2018 at 6:49
Not entirely sure what you're doing, but the answer is probably $form_state->getValues() or $form_state->getValue(). There is also $form_state->getUserInput(), but as you'll see on that page, it recommends you use one of the first 2 options unless you have a specific reason and know what you're doing.