Adding role radio button on register form
Created 7 years ago by lckamal

I am trying to add a radio button to select type of registered user in register form like this:

{% set registerForm = form('register').fields(['roles', 'email', 'username', 'first_name', 'last_name', 'password']).get() %}
            {% set activeRole = registerForm.fields.roles.value.count ? registerForm.fields.roles.value[0] : 2 %}
            {% set roles = entries('users', 'roles').whereIn('slug', ['employer', 'user']).orderBy('slug', 'desc').get() %}

            {{ registerForm.open|raw }}
            <div class="form-group {% if errors.has('roles') %}has-error{% endif %}">
                <div class="btn-group" data-toggle="buttons">
                        {% for role in roles %}
                      <label class="btn {% if role.id == activeRole %}active{% endif %}">
                        <input type="radio" name="roles[]" value="{{ role.id }}" {% if role.id == activeRole %}checked{% endif %}>{{ trans('theme::field.roles.'~role.slug) }}
                      </label>
                      {% endfor %}
                </div>
            </div>
                <div class="input1">
                    <input class="form-control" type="text" name="email" value="{{registerForm.fields.email.value}}" placeholder="{{ trans('theme::field.email.name') }}">
                    <img src="{{ asset_path('theme::img/input-img0.png') }}" class="fa-input1">
                </div>
                <div class="input1">
                    <input class="form-control" type="text" name="username" value="{{registerForm.fields.username.value}}" placeholder="{{ trans('theme::field.username.name') }}">
                    <img src="{{ asset_path('theme::img/input-img1.png') }}" class="fa-input1">
                </div>
                <div class="input2">
                    <input class="form-control" type="text" name="first_name" value="{{registerForm.fields.first_name.value}}" placeholder="{{ trans('theme::field.first_name.name') }}">
                    <img src="{{ asset_path('theme::img/input-img2.png') }}" class="fa-input2">
                </div>
                <div class="input1">
                    <input class="form-control" type="text" name="last_name" value="{{registerForm.fields.last_name.value}}" placeholder="{{ trans('theme::field.last_name.name') }}">
                    <img src="{{ asset_path('theme::img/input-img1.png') }}" class="fa-input1">
                </div>
                <div class="input2">
                    <input class="form-control" type="password" name="password" value="" placeholder="{{ trans('theme::field.password.name') }}">
                    <img src="{{ asset_path('theme::img/input-img2.png') }}" class="fa-input2">
                </div>
                <button type="submit" class="btn site-btn btn-register">GET STARTED</button>
            {{ registerForm.close|raw }}

It gives such error: http://take.ms/myj4s

Integrity constraint violation: 1062 Duplicate entry '35-2' for key 'PRIMARY' (SQL: insert into `default_users_users_roles` (`entry_id`, `related_id`) values (35, 2))

looks like value is posting double. In database first value is already saved.

In other project it was working fine with select input type: almidan.skrollex.com/register

lckamal  —  7 years ago Best Answer

I set protected $roles = [] on RegisterFormBuilder so that role will be taken from input and its fixed.

ryanthompson  —  7 years ago

In the MultipleFieldType class - can you replace the getPostValue with this:

public function getPostValue($default = null)
    {
        if (is_array($value = parent::getPostValue($default))) {
            return array_unique(array_filter($value));
        }

        return array_unique(array_filter(explode(',', $value)));
    }

And let me know if that works?

ryanthompson  —  7 years ago

Note the array_unique was added.

lckamal  —  7 years ago

Yes, I tried by adding above code but doesn't work

lckamal  —  7 years ago

@ryanthompson - is this something to do in core?

ryanthompson  —  7 years ago

Yes it was to replace a method in the field type. Ill try using your code to test this on my end and come up with a patch!

lckamal  —  7 years ago

@ryanthompson - Is this fixed? Shall I composer update?

ryanthompson  —  7 years ago

I have not been working on it - if you can submit a PR or let me know of a fix I'd happily get it addressed immediately! Been a little busy on other items. I suspect in the Multiple field type though there are multiple IDs getting into the MultipleFieldTypeAccessor where it runs sync($value)

lckamal  —  7 years ago Best Answer

I set protected $roles = [] on RegisterFormBuilder so that role will be taken from input and its fixed.