0

I want to make roles to be available only for admin can select roles for users and while user wants to edit the profile information can only see his roles but can't edit his role how to make it roles field to be locked for users Following code :

<div class="form-group"> 
{{ Form::label('role', 'Roles', array('class' => 'control-label mb-1')) }} <br/> 
{{ Form::select('roles[]',$roles,$selectedRoles,['class'=>'myselect','data-placeholder'=>'Select role(s)', 'multiple'] ) }} 
</div>
1
  • Please include details on how that isn't working. Commented Mar 7, 2020 at 4:34

1 Answer 1

3

You may use the following

use Illuminate\Validation\Rule;
//..

if(!$request->filled('roles')) {
   $request->merge(['roles' => []]);
}

$this->validate($request,[
    'name' => 'required',
    'email' => 'required|email', //'required|email|unique:users,email',
    'password' => [ 'string', 'min:8'],
    'roles' => 'nullable|array', 
    'roles.*' => [Rule::requiredIf($request->filled('roles')), 'exists:roles,id'],  
],[
    'name.required' => "Name field is required",
    'email.required' => "Email Field is Required",

    'email.email' => "Invalid Email Format ",

    'password.min' => "The Password Must be at Least 8 Characters or More",
    'roles.*' => "The Role is Required",
]);

//... 
Sign up to request clarification or add additional context in comments.

3 Comments

it still gives me same error " Invalid argument supplied for foreach() "
thank you it works , but i want to make roles to be available only for admin can select roles for users and while user wants to edit the profile information can see his roles but can't edit his role how to make it locked for users Following code : <div class="form-group"> {{ Form::label('role', 'Roles', array('class' => 'control-label mb-1')) }} <br/> {{ Form::select('roles[]',$roles,$selectedRoles,['class'=>'myselect','data-placeholder'=>'Select role(s)', 'multiple'] ) }} </div>
Glad to help, could you please edit the original post, so it can be much more readable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.