rydurham / Sentinel

A Sentry bridge package for Laravel
http://www.ryandurham.com/projects/sentinel/
317 stars 68 forks source link

How to make additional fields required during registration? #219

Closed demobiel closed 7 years ago

demobiel commented 7 years ago

Hi,

I am really not able to find out how to make additional fields required during registration? I have the feeling if I am reading your code, you first register the user, and after that add the additional fields and save the user again.

Is there an easy way to make fields required during the registration process?

thanks in advance

Andries

rydurham commented 7 years ago

Hello Andries,

Good question. Unfortunately there is not really an easy way to do this. If you are only concerned about doing validation on the additional data, you could try binding a custom FormRequest class to overwrite the Sentinel\FormRequests\UserCreateRequest class to allow for custom validation rules. I have not tried this before, so I am not sure if it will work.

If you are trying to reduce the number of database calls, you will probably want to set up custom routing and send the registration form submission to a custom route in your app that does exactly what you want.

~Ryan

demobiel commented 7 years ago

I will try :) but I think this is way above what I understand from Laravel until now

thanks for your real quick reply though.

I will keep you posted about my solution.

kind regards!

demobiel commented 7 years ago

hi

it is actually easy in your code, now I need to find out how to do it with binding...

I adpated now in the vendor folder (I know bad idea...) the file RegisterRequest to the following:

        $additional_rules = config('sentinel.additional_user_fields');
        $rules = array_merge($rules,$additional_rules);
        return $rules;
demobiel commented 7 years ago

got it working, here is my custom class request ( i placed it in services, as I have no idea where to really put it......)

<?php

namespace App\Services;

use Illuminate\Foundation\Http\FormRequest;

class CustomRegisterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'email' => 'required|min:4|max:254|email',
            'password' => 'required|min:8|confirmed',
            'password_confirmation' => 'required',
            'username' => 'unique:users,username'
        ];

        // If Usernames are enabled, add username validation rules
        if (config('sentinel.allow_usernames')) {
            $rules['username'] = 'required|unique:users,username';
        }
        $additional_rules = config('sentinel.additional_user_fields');
        $rules = array_merge($rules,$additional_rules);
        return $rules;
    }
}