Show form message only on initial load, but not after submit I'm often using Drupal::messenger() to show status/warning messages before I submit the form. But those messages are usually counter-intuitive once the form has been submitted. How can I add a message to a form that is only rendered before the form has been submitted.
My typical code looks like this:
function buildForm() {
  /**
   * ...build form...
   */
  $showWarning = $this-&gt;doSomeChecks();

  /**
   * tried to use isSubmitted() to prevent the message after submitting, but does not work
   */
  if ($showWarning  &amp;&amp; !$form_state-&gt;isSubmitted()) {
    $this-&gt;messenger()-&gt;addWarning('Some info you should know before submitting this form.');
  }
}

function submitForm(array &amp;$form, FormStateInterface $form_state) {
  /**
   * ...do stuff...
   */
  $this-&gt;messenger()-&gt;addMessage('Successfully done something');

  /**
   * tried to use deleteByType() to remove message from buildForm after submitting, but does not work
   */
  $this-&gt;messenger()-&gt;deleteByType($this-&gt;messenger()::TYPE_WARNING);
}

I know I could use $form_state-&gt;setRedirect($customPage) in submitForm(), but I don't want the overhead of creating a new page just to hide a simple warning message.
Is there no simple way to show a message only once on the initial form load, but not after submit?
Try
if (empty($form_state-&gt;getUserInput())) {
  $this-&gt;messenger()-&gt;addWarning('Some info you should know before submitting this form.');  
}

See the discussions here: Avoid repetitive API calling in form rebuild while submitting