<p>The <code>user</code> variable is only passed to the template as a variable, but not set as a global variable in the Twig environment. This means that only the top-level template being rendered has access to it. If you use the <code>include()</code> function, the current context (variables) will be passed to the included template by default, so the <code>user</code> variable will be also be available in the included template. However, if you use <code>with_context = false</code> in the <code>include()</code> function, the context will <em>not</em> be passed down to the included template. The same applies to the <code>{% embed %}</code> tag with the <code>with {} only</code> keyword at the end. That's why the <code>user</code> variable may not be available in an included or embedded template.</p>
<p>If you prefer to use <code>with_context = false</code> so included templates only have access to variables that you pass explicitly, here's a solution that will work regardless of the current context. The <code>user</code> variable is set by the <a href="https://docs.craftcms.com/api/v4/craft-controllers-userscontroller.html" rel="nofollow noreferrer"><code>UsersController</code></a> through the <a href="https://docs.craftcms.com/api/v4/craft-web-urlmanager.html" rel="nofollow noreferrer"><code>UrlManager</code></a> as a route parameter. You can access the <code>UrlManager</code> instance directly and get the <code>user</code> model from there:</p>
<pre class="lang-twig prettyprint-override"><code>{% set routeParams = craft.app.getUrlManager().getRouteParams() %}
{% set user = routeParams.user ?? null %}
</code></pre>

CC BY-SA 4.0
2023-09-29T12:10:20.403Z
2023-09-29T12:10:20.403Z
MoritzLost
<p>I've activated public user registration using the register.twig template mentioned in the documentation: <a href="https://craftcms.com/knowledge-base/front-end-user-accounts#registration-form" rel="nofollow noreferrer">https://craftcms.com/knowledge-base/front-end-user-accounts#registration-form</a></p>
<p>Unfortunately the user variable doesn't get passed back if for any reason the user account cannot be created.</p>
<p>I'm using CraftCMS version 4.4 and can't find the issue.</p>
<p>Displaying flashes works fine, but it only tells the user, that the account cannot be created without individual validation errors.</p>

CC BY-SA 4.0
2023-09-28T19:29:21.697Z
Can you please post the code you're using? Also, are you using `include()` to display the form? If so, post that code as well.
MoritzLost
CC BY-SA 4.0
2023-09-29T06:23:59.550Z
Thank you so much! That was the needed hint. I was including the form over several includes and at the beginning declared the passed variables individually and added an "only" to strip the rest. Just added "user: user ?? null" to the variables and now it works as expected.
Marc
CC BY-SA 4.0
2023-09-29T12:12:34.460Z
Had a feeling that was the issue, I've encountered that a couple of times as well :) I've added an answer explaining the cause of the issue in case anyone else is looking for it, as well as an alternative solution that will work regardless of the context.
MoritzLost
<registration><validation>
Front-end user registration doesn't return validation errors
# Front-end user registration doesn't return validation errors

(Asked by: username_0 on 2023-09-28)

I've activated public user registration using the register.twig template mentioned in the documentation: <https://craftcms.com/knowledge-base/front-end-user-accounts#registration-form>

Unfortunately the user variable doesn't get passed back if for any reason the user account cannot be created.

I'm using CraftCMS version 4.4 and can't find the issue.

Displaying flashes works fine, but it only tells the user, that the account cannot be created without individual validation errors.

**Tags:** registration, validation
> **Comments:**
> **username_1**: Can you please post the code you're using? Also, are you using `include()` to display the form? If so, post that code as well.  
> **username_0**: Thank you so much! That was the needed hint. I was including the form over several includes and at the beginning declared the passed variables individually and added an "only" to strip the rest. Just added "user: user ?? null" to the variables and now it works as expected.  
> **username_1**: Had a feeling that was the issue, I've encountered that a couple of times as well :) I've added an answer explaining the cause of the issue in case anyone else is looking for it, as well as an alternative solution that will work regardless of the context.  

---

## 1 answers

### Answer by username_1 on 2023-09-29. Score: 1 | Accepted answer

The user variable is only passed to the template as a variable, but not set as a global variable in the Twig environment. This means that only the top-level template being rendered has access to it. If you use the include() function, the current context (variables) will be passed to the included template by default, so the user variable will be also be available in the included template. However, if you use with_context = false in the include() function, the context will not be passed down to the included template. The same applies to the {% embed %} tag with the with {} only keyword at the end. That's why the user variable may not be available in an included or embedded template.
If you prefer to use with_context = false so included templates only have access to variables that you pass explicitly, here's a solution that will work regardless of the current context. The user variable is set by the UsersController through the UrlManager as a route parameter. You can access the UrlManager instance directly and get the user model from there:
{% set routeParams = craft.app.getUrlManager().getRouteParams() %}
{% set user = routeParams.user ?? null %}


---

