Problem/Motivation

If you add a form validator to a submit button, all the default form validators are bypassed.

Yur custom submit validation handler is added to the form_state->getValidateHandlers().

Here is the related code in Drupal FormValidator class:

  public function executeValidateHandlers(&$form, FormStateInterface &$form_state) {
    // If there was a button pressed, use its handlers.
    $handlers = $form_state->getValidateHandlers();
    // Otherwise, check for a form-level handler.
    if (!$handlers && isset($form['#validate'])) {
      $handlers = $form['#validate'];
    }

    foreach ($handlers as $callback) {
      call_user_func_array($form_state->prepareCallback($callback), [&$form, &$form_state]);
    }
  }

Steps to reproduce

Got hit while trying to alter a node edit form and add another custom form validator.

I was getting this error message:

Uncaught PHP Exception Drupal\\Core\\Entity\\EntityStorageException: "Entity validation is required, but was skipped." at /var/www/html/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php line 815,
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mably created an issue. See original summary.

  • mably committed 6bc0e95f on 4.x
    feat: #3563701 Form validators should not be defined on submit buttons...
mably’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.