2

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(&$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 > then the last submitted value
        */
       function start_drupal_waarde_voorwerp_form_validate($form, FormStateInterface &$form_state) {
         // Look for the just submitted value.
         $nieuwste_waarde = $form_state->getValue('field_nieuwe_waarde'); 
         // Look for all early submitted values.
         $alle_waarden = $form_state->getUserInput('field_nieuwe_waarde');
         // Search for the last submitted value.
         $laatste_bod = end($alle_waarden); 
         if ($nieuwste_waarde <= $laatste_waarde) {
           $form_state->setErrorByName( t('Gelieve hoger dan'), $laatste_waarde, t('in te stellen'));
         } 
       }
2
  • Can you confirm that you code makes to the comparison function by printing something with 'var_dump' or 'kint'?
    – ssibal
    Oct 12, 2018 at 9:35
  • The function is working and validating. Its printing after validating, but it is not getting the values from getUserInput
    – Belba
    Oct 12, 2018 at 10:34

1 Answer 1

3

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

2
  • Considering that Drupal 8 has a REST API, this is the correct way of validating an entity field.
    – apaderno
    Oct 15, 2018 at 14:59
  • Where the hook_form_alter method adds a "form" validator; it looks like Constraints are added to fields. So if i want to do a test like "make sure 1 of these 4 fields has a selection"; then this becomes very difficult with constraints, correct?
    – liquidcms
    Jan 6, 2020 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.