<p>I have a Category "Colors".
This category has: Blue, Red, Green</p>

<p>I've added a custom users field (CP > Settings > Users > Fields), this field is the category type. "Colors".
In the front of website I need users to select their faviourte color/s and save/update it.</p>

<p>Below code saves successfully "Settings saved!", but I go into the CP > Users > user@gmail.com > (custom field category colors)
and the colors have not been selected:</p>

<p><a href="https://i.sstatic.net/zWLdi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zWLdi.png" alt="enter image description here"></a></p>

<pre><code>    public function actionUpdateSettings()
    {
        $this-&gt;requirePostRequest();

        $request            = Craft::$app-&gt;getRequest();
        $myUser             = Craft::$app-&gt;getUser()-&gt;getIdentity();
        $cat                = $myUser-&gt;getAttributes()['colors']-&gt;all(); // Get users seletected colors
        $availableColors    = Category::find()-&gt;group('colors')-&gt;all(); // Query all available colors

        // Organise array for users already seletected color items
        $usersCurrentColors = array();
        foreach($cat as $item)
        {
            array_push($usersCurrentColors, $item['slug']);
        }

        // Organise all available colors to tailor to format
        $organiseAvailableColors = array();
        foreach($availableColors as $item)
        {
            $organiseAvailableColors[] = $item['slug'];
        }

        $allAvailableColors       = array_fill_keys($organiseAvailableColors, '');

        // Organise new selected colors from the form(post data)
        $newSelectedColors  = array_intersect_key($request-&gt;getBodyParams(),$allAvailableColors);

        // Set the new data that soon will be saved
        $myUser-&gt;setAttributes(array(
            'colors' =&gt;  Category::find()-&gt;group('colors')-&gt;slug($newSelectedColors),

        ), false);

        // Save data and create error messages
        $save = Craft::$app-&gt;getElements()-&gt;saveElement($myUser);

        if($save){

            return Craft::$app-&gt;session-&gt;setFlash('success_userSettings', 'Settings saved!');

        }else{
            return Craft::$app-&gt;session-&gt;setFlash('success_userSettings', 'Settings failed, please contact support!');
        }

    }
</code></pre>

<p>Craft Pro: 3.4.6.1</p>

CC BY-SA 4.0
2020-02-26T12:33:18.670Z
What type field are you using on the front end form and can users select multiple categories?
Oli
CC BY-SA 4.0
2020-03-01T21:51:51.580Z
@Olii Checkbox on front end.
Jessica Talzor
<plugin-development><craft3>
Update category selection in user custom fields
# Update category selection in user custom fields

(Asked by: username_0 on 2020-02-24)

I have a Category "Colors".
This category has: Blue, Red, Green
I've added a custom users field (CP > Settings > Users > Fields), this field is the category type. "Colors".
In the front of website I need users to select their faviourte color/s and save/update it.
Below code saves successfully "Settings saved!", but I go into the CP > Users > user@gmail.com > (custom field category colors)
and the colors have not been selected:

    public function actionUpdateSettings()
    {
        $this->requirePostRequest();

        $request            = Craft::$app->getRequest();
        $myUser             = Craft::$app->getUser()->getIdentity();
        $cat                = $myUser->getAttributes()['colors']->all(); // Get users seletected colors
        $availableColors    = Category::find()->group('colors')->all(); // Query all available colors

        // Organise array for users already seletected color items
        $usersCurrentColors = array();
        foreach($cat as $item)
        {
            array_push($usersCurrentColors, $item['slug']);
        }

        // Organise all available colors to tailor to format
        $organiseAvailableColors = array();
        foreach($availableColors as $item)
        {
            $organiseAvailableColors[] = $item['slug'];
        }

        $allAvailableColors       = array_fill_keys($organiseAvailableColors, '');

        // Organise new selected colors from the form(post data)
        $newSelectedColors  = array_intersect_key($request->getBodyParams(),$allAvailableColors);

        // Set the new data that soon will be saved
        $myUser->setAttributes(array(
            'colors' =>  Category::find()->group('colors')->slug($newSelectedColors),

        ), false);

        // Save data and create error messages
        $save = Craft::$app->getElements()->saveElement($myUser);

        if($save){

            return Craft::$app->session->setFlash('success_userSettings', 'Settings saved!');

        }else{
            return Craft::$app->session->setFlash('success_userSettings', 'Settings failed, please contact support!');
        }

    }

Craft Pro: 3.4.6.1

**Tags:** plugin-development, craft3

> **Comments:**
> **username_1**: What type field are you using on the front end form and can users select multiple categories?  
> **username_0**: @username_1i Checkbox on front end.  

---

## 0 answers

