<p>Errors for the <code>users/save-user</code> action are recorded against the <code>user</code> object, not the session object. If the user is logged-out, the object is named <code>user</code> but if they are logged-in you must use <code>currentUser</code>.</p>
<p>Generally speaking you can retrieve errors from Yii objects in several ways, but for field-by-field errors, I prefer the simplicity of <a href="https://www.yiiframework.com/doc/api/2.0/yii-base-model#getFirstError()-detail" rel="nofollow noreferrer">getFirstError()</a> (You might also want to use <code>getErrors()</code> or <code>getErrorSummary()</code>). Here's what I'm using for each question:</p>
<pre><code>{% set user = currentUser ?? user ?? null %}
...

&lt;li class=&quot;{{ user is defined and user.getFirstError('email') ? 'has-error' }}&quot;&gt;
    &lt;label for=&quot;email&quot;&gt;Email &lt;em&gt;&amp;bull;&lt;/em&gt;&lt;/label&gt;
    &lt;input required id=&quot;email&quot; type=&quot;email&quot; name=&quot;email&quot; value=&quot;{{ user is defined ? user.email }}&quot;&gt;
    {% if user is defined and user.getFirstError('email') %}
        {% include '_partials/fieldErrors.twig' with { errors: user.getErrors('email') } %}
    {% endif %}
&lt;/li&gt;
</code></pre>
<p>(You could of course use a macro instead of an include as many examples demonstrate).</p>
<pre><code>fieldErrors.twig:

{% if errors %}
    &lt;ul&gt;
        {% for error in errors %}
            &lt;li&gt;{{ error }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endif %}
</code></pre>

CC BY-SA 4.0
2020-07-07T10:34:13.463Z
Hmm I think there must be something not quite configured correctly somewhere with my install then. 

Using the exact form example on the docs mentioned above returns no errors at all for the fields using your example, just the generic flash message.

If I dump out `user.getErrors()` or `user.getFirstError('email')` when I've intentionally left an email field blank or put a random string in it always just returns an empty array.

I'll keep digging! Thanks for taking the time to reply though
Sk446
CC BY-SA 4.0
2020-07-07T10:46:47.683Z
Ah yes apologies, my example is for logged-out users. For logged-in users like the example form in the docs, you'll need to use `currentUser`. I'll update the answer.
James Smith
CC BY-SA 4.0
2020-07-06T17:09:56.803Z
2020-07-07T10:49:55.937Z
2020-07-07T10:49:55.937Z
James Smith
<p>I've been implementing a user account page with Craft Commerce and copied over the template found here: <a href="https://docs.craftcms.com/v3/dev/examples/user-profile-form.html#simple-profile" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/examples/user-profile-form.html#simple-profile</a></p>
<p>The issue is though that the form doesn't seem to show any errors for individual fields, you just get the generic error message in the flash response.</p>
<p>If you take the example from the above link and enter an intentionally invalid email address (e.g a random string) theres no error response for the email field, just a generic &quot;Couldn't Save User.&quot; response.</p>
<p>I've tried dumping out <code>{{ craft.app.session.get('error') }} </code> in an attempt to see if theres some extra data and its just the above error.</p>
<p>Am I missing something here, or is it just not possible to get field based error messages out of Craft CMS / Craft Commerce?</p>

<plugin-craftcommerce><form><error-message><validation><error-handling>
Missing field validation on user account edit
# Missing field validation on user account edit

(Asked by: username_0 on 2020-07-06)

I've been implementing a user account page with Craft Commerce and copied over the template found here: <https://docs.craftcms.com/v3/dev/examples/user-profile-form.html#simple-profile>

The issue is though that the form doesn't seem to show any errors for individual fields, you just get the generic error message in the flash response.

If you take the example from the above link and enter an intentionally invalid email address (e.g a random string) theres no error response for the email field, just a generic "Couldn't Save User." response.

I've tried dumping out `{{ craft.app.session.get('error') }}`  in an attempt to see if theres some extra data and its just the above error.

Am I missing something here, or is it just not possible to get field based error messages out of Craft CMS / Craft Commerce?

**Tags:** plugin-craftcommerce, form, error-message, validation, error-handling

---

## 1 answers

### Answer by username_1 on 2020-07-06. Score: 1 | Accepted answer

Errors for the users/save-user action are recorded against the user object, not the session object. If the user is logged-out, the object is named user but if they are logged-in you must use currentUser.
Generally speaking you can retrieve errors from Yii objects in several ways, but for field-by-field errors, I prefer the simplicity of getFirstError() (You might also want to use getErrors() or getErrorSummary()). Here's what I'm using for each question:
{% set user = currentUser ?? user ?? null %}
...


    Email •
    
    {% if user is defined and user.getFirstError('email') %}
        {% include '_partials/fieldErrors.twig' with { errors: user.getErrors('email') } %}
    {% endif %}


(You could of course use a macro instead of an include as many examples demonstrate).
fieldErrors.twig:

{% if errors %}
    
        {% for error in errors %}
            {{ error }}
        {% endfor %}
    
{% endif %}

> **Comments:**
> **username_0**: Hmm I think there must be something not quite configured correctly somewhere with my install then.   Using the exact form example on the docs mentioned above returns no errors at all for the fields using your example, just the generic flash message.  If I dump out `user.getErrors()` or `user.getFirstError('email')` when I've intentionally left an email field blank or put a random string in it always just returns an empty array.  I'll keep digging! Thanks for taking the time to reply though  
> **username_1**: Ah yes apologies, my example is for logged-out users. For logged-in users like the example form in the docs, you'll need to use `currentUser`. I'll update the answer.  

---

