Im trying to add a dropdown list on my registration form in laravel so that a user can select what type of functionality they have. for example there will be two options one being a student where they can only edit/delete their own content once logged in and one being Admin where the admin can edit/delete all content.
At the moment i have managed to add the dropdown list to the form and it is appearing in the form but when i click on register i get an error message saying the 'role' field is required even though i have selected a role:
like this:
the Role field was selected, it deselects when the error pops up.
This is my code within my registration.blade.php file:
<div class="form-group row">
<label for="role" class="col-md-4 col-form-label text-md-right">{{ __('Role') }}</label>
<div class="col-sm-6">
<select class="form-control" id="role" name="role_selected" required focus>
<option value="Admin" selected>Admin</option>
<option value="Student" selected>Student</option>
<option value="Select Role" disabled selected>Select Role</option>
</select>
</div>
</div>
And when i make the role field so that is not a required field i get this error:
Undefined index: role
and this is my code in my registerController and user table:
RegisterController.php:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password'=> ['required', 'string', 'min:8', 'confirmed'],
'role',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
]);
}
}
My create users table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('role');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Is there something that i have forgotten to add in my html file because im not sure why its not working?
in blade file
<div class="form-group row">
<label for="role" class="col-md-4 col-form-label text-md-right">{{ __('Role') }}</label>
<div class="col-sm-6">
<select class="form-control" id="role" name="role" required focus>
<option value="Admin">Admin</option>
<option value="Student">Student</option>
<option value="" disabled selected>Select Role</option>
</select>
</div>
Controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password'=> ['required', 'string', 'min:8', 'confirmed'],
'role' => ['required', 'string'],
]);
}
Try this may be it's work