Using FormState::setError() in Form::submitForm() I am writing a form in Drupal 8 that provides user values to a web service, which will either act on them or send back an error.

How can I display that error message to the user?

Here's the situation in code:

class FooForm extends BarForm implements FormInterface {

  protected $fooClient;

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &amp;$form, FormStateInterface $form_state {

    $result = $this-&gt;fooClient-&gt;post($form_state-&gt;getValue('bar'));

    if ($result-&gt;hasProblems()) {
      // This doesn't seem correct: Posting to a web service is not idempotent.  
      $form_state-&gt;setErrorByName('bar', $this-&gt;t('Problems.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {

    $result = $this-&gt;fooClient-&gt;post($form_state-&gt;getValue('bar'));

    if ($result-&gt;hasProblems()) {
      // This throws a LogicException.
      $form_state-&gt;setErrorByName('bar', $this-&gt;t('Problems.'));
    }
  }
}


Drupal treats 'validate' and 'submit' as two separate steps, but the web service doesn't. The only way to determine if the values are fully 'valid' is to get the web service to look at them, which is an operation that potentially has side effects.

validateForm() and other form validation handlers are not supposed to contain business logic, since they may get called multiple times during the life of the form, particularly if AJAX is involved. 

However, all errors are meant to be resolved before submitForm() and any other submission handlers are called. FormState::setError() enforces this by throwing a LogicException if you try to create any. Also, submitForm() clears away the user's values, and the next page builds the form from scratch.

I've seen this similar question for Drupal 7. Back then, it was possible (if discouraged and ugly) to use form_set_error() in the submit handler or the form builder. As far as I can tell, that flat-out doesn't work in Drupal 8.

How am I supposed to interact with RESTful web services? Is there a way to pass errors up from a form submit handler? Or is there a way to ensure a validation handler runs once, only once, and only if all other validation passes? Or is there some way to do this with multi-step forms?
The Drupal form system, isn't really built for what you are trying to do. I see two ways of going about this:


Webservice validation

If the webservice can do a validation of the data or such a method can be added, you can use the form system as is, and use ::validateForm to validate via webservice.
Rebuild form

What you can do, instead of doing a "validation" is to delete your ::validateForm method entirely and just send the data to the webservice in the ::submitForm. If the operation fails, you can rebuild the form and display a message to the user. You can make it appear as a validation error even though it technically isn't. Drupal does something like this as well. Fx in NodeForm::save

public function save(array $form, FormStateInterface $form_state) {
  ...
  $node-&gt;save();
  ...
  if ($node-&gt;id()) {
    ...
  else {
    // In the unlikely case something went wrong on save, the node will be
    // rebuilt and node form redisplayed the same way as in preview.
    drupal_set_message(t('The post could not be saved.'), 'error');
    $form_state-&gt;setRebuild();
  }
}


So basically, you can use $form_state-&gt;setRebuild() and drupal_set_message to make something that mimics validation in the submit handler. You can also save data in the store ($form_state-&gt;setStorate($data)) which you can use the form method to display error message or something else to the user.