Skip to main content
added 147 characters in body
Source Link

Assuming you're using the core users/save-user action to edit users (as per the official User Profile form example) you can add a form input named groups to save group assignments from athe frontend form.

Note that this only works when editing an existing user's profile, i.e. not when registering new users.

Assuming you're using the core users/save-user action (as per the official User Profile form example) you can add a form input named groups to save group assignments from a frontend form.

Assuming you're using the core users/save-user action to edit users (as per the official User Profile form example) you can add a form input named groups to save group assignments from the frontend form.

Note that this only works when editing an existing user's profile, i.e. not when registering new users.

added 19 characters in body
Source Link

Assuming you're using the core 'usersusers/save-user'user action (as per the official User Profile form example) you can add a form input named groups to save group assignments from a frontend form.

AgainBefore saving the user, Craft will handle permission checking for you (again, assuming you're posting the form to the core 'usersusers/save-user' controller action, Craft will handle permission checking for you before the user is savedaction). ButStill, you shouldit's probably avoid exposing any usera good idea to make sure that only groups as options, that the current user isn't actually permitted to assign usersis able to save assignments for, are exposed as options in the form. OneHere's one way to doachieve that is– first pulling all user groups via the craft.app.userGroups service, and then filtering those groups, using the currentUser.can() method within a Twig |filter, checkingto check that the current user has the necessary assignUserGroup:groupUid[UserGroupUID] permission for each group:

Or if you prefer, checkboxes (notice. Notice the hidden input name="groups" above the actual checkbox inputs;inputs – that's there to make it possible to submit the form without any groups checked):

Assuming you're using the core 'users/save-user' action (as per the official User Profile form example) you can add a form input named groups to save group assignments from a frontend form.

Again, assuming you're posting the form to the core 'users/save-user' controller action, Craft will handle permission checking for you before the user is saved. But, you should probably avoid exposing any user groups as options, that the current user isn't actually permitted to assign users to. One way to do that is using the currentUser.can() method within a Twig |filter, checking the assignUserGroup:groupUid permission for each group:

Or if you prefer, checkboxes (notice the hidden input name="groups" above the actual checkbox inputs; that's there to make it possible to submit the form without any groups checked):

Assuming you're using the core users/save-user action (as per the official User Profile form example) you can add a form input named groups to save group assignments from a frontend form.

Before saving the user, Craft will handle permission checking for you (again, assuming you're posting the form to the core users/save-user action). Still, it's probably a good idea to make sure that only groups that the current user is able to save assignments for, are exposed as options in the form. Here's one way to achieve that – first pulling all user groups via the craft.app.userGroups service, and then filtering those groups, using the currentUser.can() method to check that the current user has the necessary assignUserGroup:[UserGroupUID] permission for each group:

Or if you prefer, checkboxes. Notice the hidden input name="groups" above the actual checkbox inputs – that's there to make it possible to submit the form without any groups checked:

added 19 characters in body
Source Link

Assuming you're using the core 'users/save-user' action (as per the official User Profile form example) you can add a form input named groups to enablesave group assignments from a frontend form.

Again, assuming you're posting the form to the core 'users/save-user' controller action, Craft will handle permission checking for you before the user is saved. But, you should probably avoid exposing any user groups as options, that the current user isn't actually permitted to assign users to. One way to do that is using the currentUser.can() method within a Twig |filter, checking the assignUserGroup:groupUid permission for each group:

{% set userGroups = craft.app.userGroups.allGroups()|filter(group => currentUser.can("assignUserGroup:#{group.uid}")) %}

Once you have your userGroups array, you can build a dropdown:

<form method="post" accept-charset="UTF-8" enctype="multipart/form-data">
    ...
    <label for="group">User group</label>
    <select id="group" name="groups[]">
        {% for userGroup in userGroups %}
            <option value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} selected{% endif %}>{{ userGroup.name }}</option>
        {% endfor %}
    </select>
    ...
</form>

Or if you prefer, checkboxes (notice the hidden input name="groups" above the actual checkbox inputs; that's there to make it possible to submit the form without any groups checked):

<input type="hidden" name="groups" value/>
<fieldset>
    <legend>User groups</legend>
    {% for userGroup in userGroups %}
        <label>
            <input type="checkbox" name="groups[]" value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} checked{% endif %} />
            <span>{{ userGroup.name }}</span>
        </label>
    {% endfor %}
</fieldset>

Or radio buttons:

<fieldset>
    <legend>User group</legend>
    {% for userGroup in userGroups %}
        <label>
            <input type="radio" name="groups[]" value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} checked{% endif %} />
            <span>{{ userGroup.name }}</span>
        </label>
    {% endfor %}
</fieldset>

Assuming you're using the core 'users/save-user' action (as per the official User Profile form example) you can add a form input named groups to enable group assignments.

Again, assuming you're posting the form to the core 'users/save-user' controller action, Craft will handle permission checking for you before the user is saved. But, you should probably avoid exposing any user groups as options, that the current user isn't actually permitted to assign users to. One way to do that is using the currentUser.can() method within a Twig |filter, checking the assignUserGroup:groupUid permission for each group:

{% set userGroups = craft.app.userGroups.allGroups()|filter(group => currentUser.can("assignUserGroup:#{group.uid}")) %}

Once you have your userGroups array, you can build a dropdown:

<form method="post" accept-charset="UTF-8" enctype="multipart/form-data">
    ...
    <label for="group">User group</label>
    <select id="group" name="groups[]">
        {% for userGroup in userGroups %}
            <option value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} selected{% endif %}>{{ userGroup.name }}</option>
        {% endfor %}
    </select>
    ...
</form>

Or if you prefer, checkboxes (notice the hidden input name="groups" above the actual checkbox inputs; that's there to make it possible to submit the form without any groups checked):

<input type="hidden" name="groups" value/>
<fieldset>
    <legend>User groups</legend>
    {% for userGroup in userGroups %}
        <label>
            <input type="checkbox" name="groups[]" value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} checked{% endif %} />
            <span>{{ userGroup.name }}</span>
        </label>
    {% endfor %}
</fieldset>

Or radio buttons:

<fieldset>
    <legend>User group</legend>
    {% for userGroup in userGroups %}
        <label>
            <input type="radio" name="groups[]" value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} checked{% endif %} />
            <span>{{ userGroup.name }}</span>
        </label>
    {% endfor %}
</fieldset>

Assuming you're using the core 'users/save-user' action (as per the official User Profile form example) you can add a form input named groups to save group assignments from a frontend form.

Again, assuming you're posting the form to the core 'users/save-user' controller action, Craft will handle permission checking for you before the user is saved. But, you should probably avoid exposing any user groups as options, that the current user isn't actually permitted to assign users to. One way to do that is using the currentUser.can() method within a Twig |filter, checking the assignUserGroup:groupUid permission for each group:

{% set userGroups = craft.app.userGroups.allGroups()|filter(group => currentUser.can("assignUserGroup:#{group.uid}")) %}

Once you have your userGroups array, you can build a dropdown:

<form method="post" accept-charset="UTF-8" enctype="multipart/form-data">
    ...
    <label for="group">User group</label>
    <select id="group" name="groups[]">
        {% for userGroup in userGroups %}
            <option value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} selected{% endif %}>{{ userGroup.name }}</option>
        {% endfor %}
    </select>
    ...
</form>

Or if you prefer, checkboxes (notice the hidden input name="groups" above the actual checkbox inputs; that's there to make it possible to submit the form without any groups checked):

<input type="hidden" name="groups" value/>
<fieldset>
    <legend>User groups</legend>
    {% for userGroup in userGroups %}
        <label>
            <input type="checkbox" name="groups[]" value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} checked{% endif %} />
            <span>{{ userGroup.name }}</span>
        </label>
    {% endfor %}
</fieldset>

Or radio buttons:

<fieldset>
    <legend>User group</legend>
    {% for userGroup in userGroups %}
        <label>
            <input type="radio" name="groups[]" value="{{ userGroup.id }}"{% if user.isInGroup(userGroup) %} checked{% endif %} />
            <span>{{ userGroup.name }}</span>
        </label>
    {% endfor %}
</fieldset>
added 100 characters in body
Source Link
Loading
Source Link
Loading