title: How to display validation errors in CakePHP 3.6?
body: <p>I'm having trouble displaying the validation errors of a form using a custom validator.
  The errors does exist as the debug method shows, it just won't be displayed in the form.</p>
  
  <p>I'd like to be able to show the error message under (or above, or anywhere) the field.</p>
  
  <h1>What I've tried</h1>
  
  <p>Well, the <a href="https://book.cakephp.org/3.0/en/views/helpers/form.html#displaying-errors" rel="nofollow noreferrer">documentation</a> does state:</p>
  
  <blockquote>
    <p>When using View\Helper\FormHelper::control(), errors are rendered by
    default, so you don’t need to use isFieldError() or call error()
    manually.</p>
  </blockquote>
  
  <p>Nevertheless, I added the following in the form (just below the email control), which didn't do anything more. No message displayed.</p>
  
  <pre><code>if ($this-&gt;Form-&gt;isFieldError('email')) {
      echo $this-&gt;Form-&gt;error('email', 'Yes, it fails!');
  }
  </code></pre>
  
  <p>I've also found several questions and answers about this issue on SO, but they look outdated (from '09 to '13) and do not seem to correspond to today's CakePHP syntax.</p>
  
  <h1>What I've done</h1>
  
  <p><strong>Users/forgot_password.ctp</strong></p>
  
  <pre><code>&lt;?= $this-&gt;Form-&gt;create() ?&gt;
  &lt;?= $this-&gt;Form-&gt;control('email', ['type' =&gt; 'email']) ?&gt;
  &lt;?= $this-&gt;Form-&gt;button(__('Reset my password')) ?&gt;
  &lt;?= $this-&gt;Form-&gt;end() ?&gt;
  </code></pre>
  
  <p><strong>UsersController.php</strong></p>
  
  <p>(notice the specific validation set, as explained in <a href="https://book.cakephp.org/3.0/en/orm/validation.html#using-a-different-validation-set" rel="nofollow noreferrer">documentation</a>)</p>
  
  <pre><code>public function forgotPassword()
  {
      if ($this-&gt;request-&gt;is('post')) {
  
          $user = $this-&gt;Users-&gt;newEntity($this-&gt;request-&gt;getData(), ['validate' =&gt; 'email']);
          if ($user-&gt;errors()) {
              debug($user-&gt;errors()); // &lt;- shows the validation error
              $this-&gt;Flash-&gt;error(__('An error occurred.'));
          } else {
  
            // ... procedure to reset password (which works fine!) and redirect to login...
  
            return $this-&gt;redirect(['action' =&gt; 'login']);
          }
      }
  }
  </code></pre>
  
  <p><strong>UsersTable.php</strong></p>
  
  <pre><code>public function validationEmail(Validator $validator)
  {
      $validator
          -&gt;email('email')
          -&gt;notEmpty('email', __('An email address is required.'));
  
      return $validator;
  }
  </code></pre>
  
  <h1>What it looks like</h1>
  
  <p><a href="https://i.stack.imgur.com/tKw58.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tKw58.png" alt="enter image description here"></a></p>
  
  <h1>Update</h1>
  
  <p>Thanks to @ndm comment, here is the correct way to display the error.</p>
  
  <p>In <strong>UsersController.php</strong>:</p>
  
  <pre><code>public function forgotPassword()
  {
      // user context for the form
      $user = $this-&gt;Users-&gt;newEntity();
  
      if ($this-&gt;request-&gt;is('post')) {
  
          $user = $this-&gt;Users-&gt;patchEntity(§user, $this-&gt;request-&gt;getData(), ['validate' =&gt; 'email']); &lt;- validation done on patchEntity
          if ($user-&gt;errors()) {
              $this-&gt;Flash-&gt;error(__('An error occurred.'));
          } else {
  
            // ... procedure to reset password and redirect to login...
  
            return $this-&gt;redirect(['action' =&gt; 'login']);
          }
      }
  
      // pass context to view
      $this-&gt;set(compact('user'));
  }
  </code></pre>
  
  <p>And in the view <strong>forgotPassword.ctp</strong>:</p>
  
  <pre><code>&lt;?= $this-&gt;Form-&gt;create($user) ?&gt;
  </code></pre>