5

Is there a function (in Drupal 8) to build an array containing the submitted values in $form state? If I use $form_state->getValues(), I get more values than the ones I need.

8

If you are getting values like form_id, form_token, or form_build_id, which you don't want, then you need to use the following code.

$values = $form_state->cleanValues()->getValues();

The documentation for FormState::cleanValues() says:

This function can be used when a module wants to store all submitted form values, for example, by serializing them into a single database column. In such cases, all internal Form API values and all form button elements should not be contained, and this function allows their removal before the module proceeds to storage. Next to button elements, the following internal values are removed by default.

  • form_id
  • form_token
  • form_build_id
  • op

FormState::cleanValues() returns $this, so it can be used in chained calls, as in the code I showed.

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.