1

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 2

5
$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

1
  • 1
    Thanks Mykola! Yep, I missed to check the code in: Drupal\Core\Form\FormBuilder::buildForm() . Now I got it, thx!
    – Calar
    Commented Apr 12, 2018 at 6:49
1

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.

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.