How to set validation error for specific subfield of the paragraph type? 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-&gt;getValue('field_sor_rule'));
array (size=3)
  0 =&gt; 
    array (size=3)
      'subform' =&gt; 
        array (size=2)
          'field_sor_rule' =&gt; 
            array (size=5)
              0 =&gt; 
                array (size=2)
                  'value' =&gt; string '1' (length=1)
                  '_weight' =&gt; string '0' (length=1)


I've tried already referencing it as:

$form_state-&gt;setErrorByName("field_sor_rule[0][subform][field_sor_rule][0]",
  t('Please enter a valid value!'));


and:

$form_state-&gt;setErrorByName("field_sor_rule_0_subform_field_sor_rule_0]",
  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-&gt;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:

$form_state-&gt;setErrorByName("field_sor_rule][0][subform][field_sor_rule][0",
  t('Please enter a valid value!'));