Answers.Body: <p>After calling <code>saveElements</code>, if saving failed due to validation errors, those errors will be available through the <code>$family</code> entry. The way to show those errors to the user is to provide this model back to the template so it can display any errors next to the appropriate fields.</p>
  <pre class="lang-php prettyprint-override"><code>$success = Craft::$app-&gt;elements-&gt;saveElement($family);
  if (!$success) {
      Craft::$app-&gt;getUrlManager()-&gt;setRouteParams([
          'family' =&gt; $family,
      ]);
      return null;
  }
  </code></pre>
  <p>The key is to return <code>null</code>, which will let Craft know that the controller doesn't wish to handle the request further, which means it will fall back to rendering the template again. Starting with Craft 4, you can also use <a href="https://docs.craftcms.com/api/v5/craft-web-controller.html#method-asmodelfailure" rel="nofollow noreferrer"><code>$this-&gt;asModelFailure()</code></a> to accomplish the same thing.</p>
  <p>This is why you use <code>actionInput()</code> instead of setting the <code>action</code> parameter on the <code>&lt;form&gt;</code> element. The POST request goes to the same page the form is on, so the template with the form is rendered again, but this time, you have access to the submitted model. This model allows you to both repopulate the form with the values entered by the user, and show any pending errors.</p>
  <pre class="lang-twig prettyprint-override"><code>&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;{{ family.name ?? 'default' }}&quot;&gt;
  {% set errors = family.getErrors('name') ?? false %}
  {% if errors %}
      {# Show errors to the user #}
  {% endif %}
  </code></pre>
  <p>The key used in <code>getErrors()</code> is the name of the attribute on the model, so it can be any built-in attribute of entries (<code>title</code>, <code>postDate</code>, etc) or any custom field you have in your field layout.</p>
  <p>You can also get a list of all errors to show to the user above the form:</p>
  <pre class="lang-php prettyprint-override"><code>{% set errors = family.getErrors() %}
  {% if errors %}
      {# Show errors to the user #}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-26T07:56:22.663Z
Answers.LastActivityDate: 2024-06-26T07:56:22.663Z
Answers.UserDisplayName: MoritzLost
Body: <p>I have a form:</p>
  <pre><code>&lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
  {{ csrfInput() }}
  {{ actionInput('families/families/start') }}
  &lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;&quot;&gt;
  &lt;input type=&quot;submit&quot; value=&quot;go&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  <p>I have a controller:</p>
  <pre><code>namespace modules\families\controllers;
  
  use Craft;
  use craft\web\Controller;
  use craft\elements\Entry;
  
  class FamiliesController extends Controller
  {
      protected array|bool|int $allowAnonymous = true;
      public function actionStart() {
          $name = $this-&gt;request-&gt;getBodyParam('name');
          $host = Craft::$app-&gt;request-&gt;hostInfo;
          $title = Entry::find()-&gt;section('familySeason')-&gt;title($name)-&gt;all();
          if(count($title) == 0){
              $family = new Entry();
              $family-&gt;sectionId = 7;
              $family-&gt;enabled = false;
              $family-&gt;title = $name;
              $success = Craft::$app-&gt;elements-&gt;saveElement($family);
              if (!$success) {
                      // ...
              }
              Craft::$app-&gt;getResponse()-&gt;redirect($host.&quot;/good&quot;);
          }else{
              // ...
          }
      }
  }
  </code></pre>
  <p>My controller has a check for the uniqueness of the name field, but I do not know how to output a user error if he wants to add an entry with a name that already exists. Can you tell me how to do this?</p>
  
Tags: <craft3><assets><form><error-message><error>
Title: How can I display a message to the user that the field is filled in incorrectly?
ThreadText: # How can I display a message to the user that the field is filled in incorrectly?
  
  (Asked by: username_0 on 2024-06-21)
  
  I have a form:
  
  ```
  <form method="POST" enctype="multipart/form-data">
  {{ csrfInput() }}
  {{ actionInput('families/families/start') }}
  <input type="text" name="name" value="">
  <input type="submit" value="go">
  </form>
  
  ```
  
  I have a controller:
  
  ```
  namespace modules\families\controllers;
  
  use Craft;
  use craft\web\Controller;
  use craft\elements\Entry;
  
  class FamiliesController extends Controller
  {
      protected array|bool|int $allowAnonymous = true;
      public function actionStart() {
          $name = $this->request->getBodyParam('name');
          $host = Craft::$app->request->hostInfo;
          $title = Entry::find()->section('familySeason')->title($name)->all();
          if(count($title) == 0){
              $family = new Entry();
              $family->sectionId = 7;
              $family->enabled = false;
              $family->title = $name;
              $success = Craft::$app->elements->saveElement($family);
              if (!$success) {
                      // ...
              }
              Craft::$app->getResponse()->redirect($host."/good");
          }else{
              // ...
          }
      }
  }
  
  ```
  
  My controller has a check for the uniqueness of the name field, but I do not know how to output a user error if he wants to add an entry with a name that already exists. Can you tell me how to do this?
  
  **Tags:** craft3, assets, form, error-message, error
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-06-26. Score: 1 | Accepted answer
  
  After calling `saveElements`, if saving failed due to validation errors, those errors will be available through the `$family` entry. The way to show those errors to the user is to provide this model back to the template so it can display any errors next to the appropriate fields.
  
  ```php
  $success = Craft::$app->elements->saveElement($family);
  if (!$success) {
      Craft::$app->getUrlManager()->setRouteParams([
          'family' => $family,
      ]);
      return null;
  }
  
  ```
  
  The key is to return `null`, which will let Craft know that the controller doesn't wish to handle the request further, which means it will fall back to rendering the template again. Starting with Craft 4, you can also use [`$this->asModelFailure()`](https://docs.craftcms.com/api/v5/craft-web-controller.html#method-asmodelfailure) to accomplish the same thing.
  
  This is why you use `actionInput()` instead of setting the `action` parameter on the `<form>` element. The POST request goes to the same page the form is on, so the template with the form is rendered again, but this time, you have access to the submitted model. This model allows you to both repopulate the form with the values entered by the user, and show any pending errors.
  
  ```twig
  <input type="text" name="name" value="{{ family.name ?? 'default' }}">
  {% set errors = family.getErrors('name') ?? false %}
  {% if errors %}
      {# Show errors to the user #}
  {% endif %}
  
  ```
  
  The key used in `getErrors()` is the name of the attribute on the model, so it can be any built-in attribute of entries (`title`, `postDate`, etc) or any custom field you have in your field layout.
  
  You can also get a list of all errors to show to the user above the form:
  
  ```php
  {% set errors = family.getErrors() %}
  {% if errors %}
      {# Show errors to the user #}
  {% endif %}
  
  ```
  
  
  ---
  
  