how do i control: validateForm

public function validateForm(array &$form, FormStateInterface $form_state) {
  // form validation here
  // if validation success then proceed to submit
  // else dont submit and send notification to the respective field

i have done my fields validation inside that method, however, how do i control from there?

how to stop the submit process if the validation failed? and how to send notification/error message to the respective field?

Thank you,

  • You should share your actual code (at least one example) because that's exactly what happens if you add the validation errors to form state with the corresponding ->setError* methods.
    – Berdir
    Commented Feb 19, 2017 at 16:08

1 Answer 1

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $values = $form_state->getValues();
  if ($values['your_field'] == 'some_forbidden_value') {
      $form_state->setErrorByName('your_field', $this->t('This value is forbidden!'));

About setErrorByName().

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.