Filgard
Filgard

Reputation: 37

How do I fix user registration on my new Laravel project?

I created a new Laravel project. I used the standard php artisan make:auth and that generated the required files and connections. I created the table in phpMyAdmin and connected the two in the .env file. I checked connections and migrations and all works well.

The problem is that when I want to create a new user and enter all the register data, the fields first_name and last_name apparently can not be read and I get an error saying "The first name field is required." Same for last name. No error on password or email.

First, here's the RegisterController:

protected function validator(array $data)
{
    return Validator::make($data, [
        'first_name' => 'required|string|max:20',
        'last_name' => 'required|string|max:30',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);
}

protected function create(array $data)
{
    return User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

register.blade.php:

<form class="form-horizontal" role="form" method="POST" action="{{   route('register') }}">
    {{ csrf_field() }}

    <div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
        <label for="first_name" class="col-md-4 control-label">First Name</label>

        <div class="col-md-6">
            <input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>

            @if ($errors->has('first_name'))
                <span class="help-block">
                    <strong>{{ $errors->first('first_name') }}</strong>
                </span>
            @endif
        </div>
    </div>

    <div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
        <label for="last_name" class="col-md-4 control-label">Last Name</label>

        <div class="col-md-6">
            <input id="last_name" type="text" class="form-control" last_name="last_name" value="{{ old('last_name') }}" required autofocus>

            @if ($errors->has('last_name'))
                <span class="help-block">
                    <strong>{{ $errors->first('last_name') }}</strong>
                </span>
            @endif
        </div>
    </div>

And migrations:

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name', 20);
            $table->string('last_name', 30);
            $table->string('email')->unique();
            $table->string('password', 30);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

I checked forums and the Laravel documentation but couldn't find a mistake.

Upvotes: 1

Views: 368

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

The issue is here:

<input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>

here instead of name="first_name", first_name="first_name" is there. Make it proper and try again. Same for last_name="last_name" also.

Explanation:

On the server side, element value is fetched using the name attribute of element. But in your case name attribute is missing which causes the validation to fails and it is showing The first name field is required and same for last_name

Upvotes: 3

Related Questions