Validation with hook_form_alter() In Drupal 8, for the Comment entity, I want to validate an integer field (stored as field_nieuwe_waarde_value).
In each comment posted for a node, it needs to be greater than the value for the previous comment.

The code doesn't give any errors, but the validation doesn't work. 

use Drupal\Core\Form\FormStateInterface;

       /**
        * Implements hook_form_alter().
        */

       function waarde_validatie_form_alter(&amp;$form, $form_state, $form_id) {
         if ($form_id == 'comment_waarde_van_dit_voorwerp__form'){
           $form['#validate'][] = 'start_drupal_waarde_voorwerp_form_validate';
         }  
       }

       /**
        * Validation form for the waarde_voorwerp form.
        *
        * Check if the value is &gt; then the last submitted value
        */
       function start_drupal_waarde_voorwerp_form_validate($form, FormStateInterface &amp;$form_state) {
         // Look for the just submitted value.
         $nieuwste_waarde = $form_state-&gt;getValue('field_nieuwe_waarde'); 
         // Look for all early submitted values.
         $alle_waarden = $form_state-&gt;getUserInput('field_nieuwe_waarde');
         // Search for the last submitted value.
         $laatste_bod = end($alle_waarden); 
         if ($nieuwste_waarde &lt;= $laatste_waarde) {
           $form_state-&gt;setErrorByName( t('Gelieve hoger dan'), $laatste_waarde, t('in te stellen'));
         } 
       }
Adding a validation handler in hook_form_alter() is the Drupal 7 method of adding validation for a field. It will still work in Drupal 8, however the preferred method of adding validation to Field API Field in Drupal 8 is to add a Constraint to the field. Constraints will be applied when the Node is created, no matter the means of creation. Adding a validation handler to the form only validates the field when the node is created using the Form API - IE through a browser. If a node is created by any other means - IE through an API or programatically, the validation is not applied. Constraints are applied no matter the method of creating the node.

Documentation on how to add a constraint can be seen here: https://www.drupal.org/docs/8/api/entity-validation-api/providing-a-custom-validation-constraint

See Method 2 - adding constraints to fields added to an entity you haven't defined, or fields added through the UI