# Drupal 8 – Inline messages validation in forms

> To understand all of it, let’s go back to the basics : The creative process of a form can be divided in two phases: The processing part, which includes the \[…\]

Source: https://antistatique.net/en/blog/drupal-inline-messages-validation-in-forms

Category: Development

# Drupal 8 – Inline messages validation in forms

Author(s)

[

![Kevin](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Kevin-300x300.jpg)



](/en/agence/kevin)

[Kevin](/en/agence/kevin)16 February 2017

**Shortcut**  
I just want [to code](#code), I don’t care about the theory.

First of all, let’s review some vocabulary:

When we talk about forms, you may have heard some different terms, such as: **Submit validation,** **_Live validation_**, **_Live inline validation_**, **_Flash messages validation_** or **_Inline message validation_**… All of them are very similar, so it might be easy to get lost.

To understand all of it, let’s go back to the basics :

The creative process of a form can be divided in two phases:

-   **The processing part**, which includes the send method and the validity rules,
-   **The visual part**, which will give feedback instructions on validations.

## Phase #1 — Choose a validation method

Validation methods are techniques and technologies used in order to check the validity of an input.

They are **2 kinds**:

**1\. Standard/Submit validations** are a classic server-side validation serving error pages and including the states of inputs. This kind of validation needs the form to be submitted before giving feedback to the user.

![Twitter inline validation -live](https://images.statique.ch/t/wp/app/uploads/2020/12/twitter-live-inline-validation.gif)

**2\. Live validations**: The check is made asynchronously, using Javascript and Ajax, which bring the user direct feedback when he/she types or leaves given fields.

![Twitter inline validation -standard](https://images.statique.ch/t/wp/app/uploads/2020/12/twitter-standard-inline-validation.gif)

## Phase #2 — Choose a look

This is the UI & UX of your validation messages. The way they will look like.

This is where your strategy takes place to give clear visual and instruction feedback on form validations.

They are two kinds:

**Flash/Growl messages**: They are often shown at the top of the form and contain all your validation messages.

**Inline messages**: are a preferable way to display validation below or near the field.

Now that we have defined these two phases, you can merge them and create a form.

This article explains the more technical/code approach of **Standard/Submit validations** with **Inline messages** on Drupal 8 forms.

If you’re interested in reading more about forms methodology or design techniques, check out this collection of articles :

**Forms Need Validation**  
[Designing inline validation and error handling](https://uxdesign.cc/forms-need-validation-2ecbccbacea1)

**Form validation best practices**  
[A case study of how to get it right](https://medium.com/@andrew.burton/form-validation-best-practices-8e3bec7d0549%C3%87)

**Inline validation in forms**   
 [Designing the experience](https://medium.com/@andrew.burton/form-validation-best-practices-8e3bec7d0549%C3%87)

**Web Form Validation**  
[Best Practices and Tutorials](https://www.smashingmagazine.com/2009/07/web-form-validation-best-practices-and-tutorials)

## Coding Standard inline validations using Inline Messages validation in Drupal 8

This part is based on the assumption that you are fully comfortable with Object Oriented Programming (OOP) and the use of the Form API with Drupal 8.

![GIF - Geek robot](https://images.statique.ch/t/wp/app/uploads/2020/12/source.gif)

**Create your form**

Begin by creating a form using the Drupal 8 Form API, this form will just display a Success **Flash/Growl message** when submitted.

**Add some validations**

Now that we have a functional form, let's add a few verifications and validations to it. The easier way is to continue and use the well known method to set an error on a field: `FormState::setErrorByName`.  
By using the `setErrorByName` of `FormState`, Drupal 8 will add every messages to the **Flash/Growl message** region as shown in the picture below:

![Formular inline - Antistatique](https://images.statique.ch/t/w_750,f_jpeg,q_70/wp/app/uploads/2020/12/growl-1024x952.png)

**Disable the cache**

Well, let's get into the heart of this article, our final goal is to replace the standard Drupal **Flash messages validation** with a more elegant method - **Inline messages validation**.

The first thing to do is to disable the form cache.

We want to rebuild the form build every time we display it and show contextual Inline messages validation.

```
// Disable caching
$form['#cache']['max-age'] = 0;
```

**Implement Inline messages validation**

Before you begin, you need to add a small dependency: `composer require symfony/property-access:^3.2`.

For now we have a beautiful form but all our validations messages are shown into a single **Flash messages validation** block atop of our form. Finally, we can now code your **Inline messages validation**. It will look like:

![Formular inline](https://images.statique.ch/t/w_750,f_jpeg,q_70/wp/app/uploads/2020/12/inline-1024x877.png)

## Go further

**Disable default Drupal validations**

Sometime you will need to disable the automatic HTML5 validations. If you keep it enabled, Drupal 8 will run some of its validations before yours, such as an email validation or just on mandatory fields. This will add every message to the Flash/Growl Bag Messages System and nothing will go through your custom validations before the Drupal ones are valid.

The result is a bad user experience. They will see 2 displays of validations for the same fields (Flash - Drupal & Inline - Yours).

```
// Disable browser HTML5 validation
$form['#attributes']['novalidate'] = 'novalidate';
```

**Keep the form after submit instead of the page top**

This is far simpler than you may think, you just have to use the ID of your form and use it as an anchor when submitting.

```
// This will generate an anchor scroll to the form when submitting
$form['#action'] = '#my-contact-form';
```

**Unique name for every fields**

In fact, at the first sight it seems like you have to keep every field with a unique name to retrieve data with `$form_state->getValue('subject')` but in reality you can configure every field to be nested using the hierarchy of your form with the following line :

```
// Submitted form values should be nested.
$form['#tree'] = TRUE;
```

And retrieve data with :

```
$form_state->getValue(‘message.subject’)
```

## More articles

Author(s)

[

![Anthony](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2024/11/anthony-1-300x300.png)



](/en/agence/anthony-salamone)

[Anthony](/en/agence/anthony-salamone)28 September 2026

### [Smashing Conference Freiburg 2026](/en/blog/smashing-conference-freiburg-2026)

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

[

![Smashing Conference Freiburg 2026 poster](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/assets/blog/smashing-2026.png)



](/en/blog/smashing-conference-freiburg-2026)

Author(s)

[

![Akira](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2025/03/akira-small-300x300.jpg)



](/en/agence/akira-kamata)

[Akira](/en/agence/akira-kamata)22 July 2026

### [Making of : “La nuit des Losers”](/en/blog/making-of-la-nuit-des-losers)

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

[

![Making of : “La nuit des Losers”](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/05/La-nuit-des-Losers-Teaser-1-768x576.png)



](/en/blog/making-of-la-nuit-des-losers)

Author(s)

[

![Pierre](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Pierre-300x300.jpg)



](/en/agence/pierre)

[Pierre](/en/agence/pierre)7 July 2026

### [Dark mode: necessity or trend](/en/blog/dark-mode-necessity-or-trend)

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

[

![Dark mode: necessity or trend](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/07/1-768x384.png)



](/en/blog/dark-mode-necessity-or-trend)
