Answers.Body: <p>Matrix blocks are elements themselves, so the individual fields of a matrix block aren't available as properties on the entry. In the entry save hook, the only thing you can add errors to (that will be picked up by the interface) is the matrix field itself, using the matrix field's handle:</p>
  <pre class="lang-php prettyprint-override"><code>use craft\elements\Entry;
  use craft\events\ModelEvent;
  use yii\base\Event;
  
  Event::on(
      Entry::class,
      Entry::EVENT_BEFORE_SAVE,
      function (ModelEvent $e) {
          $e-&gt;sender-&gt;addError('products', 'The resource ID is invalid.');
          $e-&gt;isValid = false;
      }
  );
  </code></pre>
  <p>However, those errors won't be attached to the resource ID itself, but to the matrix field, and they will be displayed below the list of matrix blocks. Not very useful.</p>
  <hr />
  <p>Instead, you want to hook into the validation of matrix blocks themselves. Entry saves will validate matrix blocks and display any errors that occur (this will also prevent the entry from being saving). So you can use <code>MatrixBlock::EVENT_AFTER_VALIDATE</code> to add your own validation to matrix blocks. Using that event, you can add validation errors using the handles of the fields inside your matrix blocks, they will be picked up and displayed in the interface automatically.</p>
  <pre class="lang-php prettyprint-override"><code>use craft\elements\MatrixBlock;
  use yii\base\Event;
  
  Event::on(
      MatrixBlock::class,
      MatrixBlock::EVENT_AFTER_VALIDATE,
      function (Event $e) {
          $e-&gt;sender-&gt;addError('resourceId', 'The resource ID is invalid.');
      }
  );
  </code></pre>
  <p>Make sure to check the matrix block's <code>type</code> and only apply your custom validation to the type with the resource ID field.</p>
  <p>If you need to access the entry the matrix block belongs to for some custom validation logic, you can get to it using <code>$e-&gt;sender-&gt;owner</code>. <a href="https://docs.craftcms.com/api/v3/craft-elements-matrixblock.html#owner" rel="nofollow noreferrer">See MatrixBlock::$owner</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-03T14:33:04.240Z
Answers.Text: thank you so much, work like a charm.
Answers.UserDisplayName: Thomas
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-03T13:37:08.130Z
Answers.LastActivityDate: 2021-11-03T13:37:08.130Z
Answers.UserDisplayName: MoritzLost
Body: <p>I try to validate some fields before saving the entry, if the field is invalid, prevent the entry from saving and return the error message, I'm doing it via Element::EVENT_BEFORE_SAVE event.</p>
  <p>It works for such a Plain Text field</p>
  <pre><code>// the attribute 'resourceTitle' is the field's handle
  $entry-&gt;addError('resourceTitle', 'The title is invalid.');
  </code></pre>
  <p><a href="https://i.sstatic.net/gycav.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gycav.png" alt="enter image description here" /></a></p>
  <p>But for a Matrix field, I don't what's the attribute, using the field handle(resourceId) is not working.</p>
  <p>the source code the field is something like this:</p>
  <pre><code>&lt;input type=&quot;text&quot; id=&quot;fields-products-blocks-167910-fields-resourceId&quot; name=&quot;fields[products][blocks][167910][fields][resourceId]&quot; value=&quot;796&quot;&gt;
  </code></pre>
  <p>I tried:</p>
  <pre><code>$entry-&gt;addError('resourceId', 'The resource id is invalid');
  $entry-&gt;addError('fields-products-blocks-167910-fields-resourceId', 'The resource id is invalid');
  $entry-&gt;addError('fields[products][blocks][167910][fields][resourceId]', 'The resource id is invalid');
  </code></pre>
  <p>All failed. not sure what's the attribute.
  <a href="https://i.sstatic.net/hFi0f.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/hFi0f.png" alt="enter image description here" /></a></p>
  
Tags: <craft3><error-message><events><entries-field><attribute>
Title: What's the attribute of a Matrix field to add error message
ThreadText: # What's the attribute of a Matrix field to add error message
  
  (Asked by: username_0 on 2021-11-03)
  
  I try to validate some fields before saving the entry, if the field is invalid, prevent the entry from saving and return the error message, I'm doing it via Element::EVENT\_BEFORE\_SAVE event.
  
  It works for such a Plain Text field
  
  ```
  // the attribute 'resourceTitle' is the field's handle
  $entry->addError('resourceTitle', 'The title is invalid.');
  
  ```
  
  [![enter image description here](https://i.sstatic.net/gycav.png)](https://i.sstatic.net/gycav.png)
  
  But for a Matrix field, I don't what's the attribute, using the field handle(resourceId) is not working.
  
  the source code the field is something like this:
  
  ```
  <input type="text" id="fields-products-blocks-167910-fields-resourceId" name="fields[products][blocks][167910][fields][resourceId]" value="796">
  
  ```
  
  I tried:
  
  ```
  $entry->addError('resourceId', 'The resource id is invalid');
  $entry->addError('fields-products-blocks-167910-fields-resourceId', 'The resource id is invalid');
  $entry->addError('fields[products][blocks][167910][fields][resourceId]', 'The resource id is invalid');
  
  ```
  
  All failed. not sure what's the attribute.
  [![enter image description here](https://i.sstatic.net/hFi0f.png)](https://i.sstatic.net/hFi0f.png)
  
  **Tags:** craft3, error-message, events, entries-field, attribute
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-11-03. Score: 1 | Accepted answer
  
  Matrix blocks are elements themselves, so the individual fields of a matrix block aren't available as properties on the entry. In the entry save hook, the only thing you can add errors to (that will be picked up by the interface) is the matrix field itself, using the matrix field's handle:
  
  ```php
  use craft\elements\Entry;
  use craft\events\ModelEvent;
  use yii\base\Event;
  
  Event::on(
      Entry::class,
      Entry::EVENT_BEFORE_SAVE,
      function (ModelEvent $e) {
          $e->sender->addError('products', 'The resource ID is invalid.');
          $e->isValid = false;
      }
  );
  
  ```
  
  However, those errors won't be attached to the resource ID itself, but to the matrix field, and they will be displayed below the list of matrix blocks. Not very useful.
  
  ---
  
  Instead, you want to hook into the validation of matrix blocks themselves. Entry saves will validate matrix blocks and display any errors that occur (this will also prevent the entry from being saving). So you can use `MatrixBlock::EVENT_AFTER_VALIDATE` to add your own validation to matrix blocks. Using that event, you can add validation errors using the handles of the fields inside your matrix blocks, they will be picked up and displayed in the interface automatically.
  
  ```php
  use craft\elements\MatrixBlock;
  use yii\base\Event;
  
  Event::on(
      MatrixBlock::class,
      MatrixBlock::EVENT_AFTER_VALIDATE,
      function (Event $e) {
          $e->sender->addError('resourceId', 'The resource ID is invalid.');
      }
  );
  
  ```
  
  Make sure to check the matrix block's `type` and only apply your custom validation to the type with the resource ID field.
  
  If you need to access the entry the matrix block belongs to for some custom validation logic, you can get to it using `$e->sender->owner`. [See MatrixBlock::$owner](https://docs.craftcms.com/api/v3/craft-elements-matrixblock.html#owner).
  
  > **Comments:**
  > **username_0**: thank you so much, work like a charm.  
  
  ---
  
  