I've got paragraphs type with two fields which I've added as a field into my vocabulary.

I've added validation function (#validate) into $form using hook_form_FORM_ID_alter where I'm validating values of these fields, however when I'm using the machine name of the paragraph field, all widgets are highlighted. My aim is to highlight only the one which fails.

The structure of that paragraph field (field_sor_rule) is like:

// var_dump($form_state->getValue('field_sor_rule'));
array (size=3)
  0 => 
    array (size=3)
      'subform' => 
        array (size=2)
          'field_sor_rule' => 
            array (size=5)
              0 => 
                array (size=2)
                  'value' => string '1' (length=1)
                  '_weight' => string '0' (length=1)

I've tried already referencing it as:

  t('Please enter a valid value!'));


  t('Please enter a valid value!'));

but without success.

What would be a valid syntax?


In the docs page for setErrorByName() we can read:

string $name: The name of the form element. If the #parents property of your form element is array('foo', 'bar', 'baz') then you may set an error on foo or foo][bar][baz. Setting an error on 'foo' sets an error for every element where the #parents array starts with 'foo'.

We should also note the following code how it's parsed:

public function setErrorByName($name, $message = '') {
  // ...
      foreach ($limit_validation_errors as $section) {
        // Exploding by '][' reconstructs the element's #parents. If the
        // reconstructed #parents begin with the same keys as the specified
        // section, then the element's values are within the part of
        // $form_state->getValues() that the clicked button requires to be
        // valid, so errors for this element must be recorded. As the exploded
        // array will all be strings, we need to cast every value of the
        // section array to string.
        if (array_slice(explode('][', $name), 0, count($section)) === array_map('strval', $section)) {

So the mentioned element should have the following syntax:

  t('Please enter a valid value!'));
  • Really? The accepted solution is where you hardcode the name of your element? Isn't this what setError for? public function setError(array &$element, $message = '') { $this->setErrorByName(implode('][', $element['#parents']), $message); return $this; } – ssibal Jul 8 '19 at 11:39

You might want to consider using Field Validation module. It's pre-release on D8 at the moment, but it was very handy when I used it on D7.


try to display error by using below method

add below namespace in ur form

use Symfony\Component\PropertyAccess\PropertyAccess;

set the error by using below method

 $form_state->setErrorByName('[quote_policy_no]','Can not submit with current date.');   

and For display the error meassage

In your validate function

    $errors = $form_state->getErrors();
   if($errors = $form_state->getErrors()){
      $accessor = PropertyAccess::createPropertyAccessor();
       $accessor->setValue($form,'[#prefix]', '<div class="error-messages">' .$errors['[quote_policy_no]']. '</div>');

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.