FormValidatorInterface.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\Core\Form;
  3. /**
  4. * Provides an interface for validating form submissions.
  5. */
  6. interface FormValidatorInterface {
  7. /**
  8. * Executes custom validation handlers for a given form.
  9. *
  10. * Button-specific handlers are checked first. If none exist, the function
  11. * falls back to form-level handlers.
  12. *
  13. * @param $form
  14. * An associative array containing the structure of the form.
  15. * @param $form_state
  16. * The current state of the form. If the user submitted the form by clicking
  17. * a button with custom handler functions defined, those handlers will be
  18. * stored here.
  19. */
  20. public function executeValidateHandlers(&$form, FormStateInterface &$form_state);
  21. /**
  22. * Validates user-submitted form data in the $form_state.
  23. *
  24. * @param $form_id
  25. * A unique string identifying the form for validation, submission,
  26. * theming, and hook_form_alter functions.
  27. * @param $form
  28. * An associative array containing the structure of the form, which is
  29. * passed by reference. Form validation handlers are able to alter the form
  30. * structure (like #process and #after_build callbacks during form building)
  31. * in case of a validation error. If a validation handler alters the form
  32. * structure, it is responsible for validating the values of changed form
  33. * elements in $form_state->getValues() to prevent form submit handlers from
  34. * receiving unvalidated values.
  35. * @param $form_state
  36. * The current state of the form. The current user-submitted data is stored
  37. * in $form_state->getValues(), though form validation functions are passed
  38. * an explicit copy of the values for the sake of simplicity. Validation
  39. * handlers can also use $form_state to pass information on to submit
  40. * handlers. For example:
  41. * $form_state->set('data_for_submission', $data);
  42. * This technique is useful when validation requires file parsing,
  43. * web service requests, or other expensive requests that should
  44. * not be repeated in the submission step.
  45. */
  46. public function validateForm($form_id, &$form, FormStateInterface &$form_state);
  47. /**
  48. * Sets a form_token error on the given form state.
  49. *
  50. * @param \Drupal\Core\Form\FormStateInterface $form_state
  51. * The current state of the form.
  52. *
  53. * @return $this
  54. */
  55. public function setInvalidTokenError(FormStateInterface $form_state);
  56. }