D8 | submitForm | Get the $_POST - vars via $form_state or via \Drupal::request()?

Solutions

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.

$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

Tags: Forms

Similar questions

How to get $_POST and $_GET parameters
I ajaxly call a route and pass data to it, In my controller, I want to get $_POST['var'] , $_POST['var2'], it seems because of using HttpFoundation in drupal 8, $_POST and $_GET are not exists in old method, How I can access $_POST parameters in custom Controller?
Programatically get the webform submission "sid" on submitForm
I am trying to programatically get the webform submission sid on submitting a webform but failed to get it ! On my customwebformhandler.php file i have the below: but i am not getting any sid value. any help please ?
Drupal 8 Jsonapi Get request throws bad request error
Setting up drupal website API using json api, when accessing the link for get resource http://example.com/jsonapi/node/article - getting error "title": "Bad Request", "status": "400", "detail": "The following query parameters violate the JSON:API spec: 'q'.",
drupal store and retrieve variable from $_POST array
This is really simple question for the user who are using DRUPAL for many years.But for me it is creating a big issue. I already made a form using the drupal form api.But after submit the form I want to store form data in the "$_POST" and print the data to another module's page.I get the data on the 2nd page(module's page where I want to show the f...
Drupal form ahah element, problem with empty $_POST
I'm making a module for ubercart. It's nearly finished, but now I'm working on the admin page. I want the admin to be able to add options, like the poll module adds choices. The problem I have is that the admin form is generated through drupal_get_form, which leaves the $_POST empty. $_POST['form_build_id'] seems to be needed to get the form from c...
Passing $_POST data from page to page in Drupal
Does anyone know how to transfer $_POST data (HTML form data) from one page to another in Drupal? I have two PHP pages I'm including in two different Drupal nodes, but I can't pass POST data between them. Is there an easy way to do this?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.