John Davidson

php - Laravel Jetstream register users with different roles

0 comments
Message:


I'm new to Laravel framework.
I have a little experience with other web frameworks, especially Django web framework.
I need to create a web application with users AND companies registration support.
I've setup the user auth/registration with laravel jetstream.
The user registration form is ok for a normal user, but for agency registration the user need to put other details that are not needed for a normal user. These data are mandatory for an agency but not for a normal user.


I thought to edit the laravel User model and add all the extra fields required for agency registration (as optional in the Model), and the Jetstram registration view to include extra fields and a field to distinguish between User and Agency. If the user select "Agency" (in the registration form) I can show extra (required) fields through Javascript. Obviously I need also a kind of backend validation.
In django framework there is a clean method (in forms and models) that performs an Object level validation, so I can do a thing like this:


def clean(self):
cleaned_data = super().clean()
my_extrafield_1 = cleaned_data['my_extrafield_1']
my_extrafield_2 = cleaned_data['my_extrafield_2']
if (userType == "agency") and not (my_extrafield_1 and my_extrafield_2):
raise forms.ValidationError('You have selected "Agency registration" but some required fields have not been provided!')

How I can do the same thing with Laravel Jetstream?
And, is this the correct way to achieve my goal?



My Answer:
Agency during registration, the extra fields will be required._I'm not sure if this is the best approach or if there is a better way to handle this scenario in Laravel with Jetstream._I would appreciate any guidance or suggestions on how to properly implement user registration with different roles using Laravel Jetstream.

Rate this post

5 of 5 based on 9102 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us