Skip to content
Advertisement

Laravel Validation – The full name field is required [closed]

I’m trying my first form validation with Laravel, and for some reason the first field (full name) won’t accept anything I type and return an error “The full name field is required”.

My form view.

<!-- CONTACT -->
<section>
    <div>
        <h3>Contact me for more information</h3>
        @if (session('status'))
        <p>{{ session('status') }}</p>
        @endif
        <form action="/contact" method="POST">
            {{ csrf_field() }}
            <div>
                <div class="relative">
                    <label for="text">Full Name</label>
                    <input type="text" name="fullname" placeholder="John Doe" value="{{ old('fullName') }}" required />
                    @error('fullName')
                    <div>{{ $message }}</div>
                    @enderror
                </div>
                <div class="relative">
                    <label for="email">E-mail</label>
                    <input type="email" name="email" placeholder="email@example.com" value="{{ old('email') }}" required />
                    @error('email')
                    <div>{{ $message }}</div>
                    @enderror
                </div>
            </div>
            <div class="relative">
                <label for="text">Phone Number</label>
                <input type="text" name="phoneNumber" placeholder="(123) 456-7890" value="{{ old('phoneNumber') }}" />
                @error('phoneNumber')
                <div>{{ $message }}</div>
                @enderror
            </div>
            <div class="relative">
                <label for="textarea">Message</label>
                @error('message')
                <div>{{ $message }}</div>
                @enderror
                <textarea type="textarea" name="message" rows="6" placeholder="Message" value="{{ old('message') }}" required></textarea>
            </div>
            <button type="submit">Send</button>
        </form>
    </div>
</section>

ContactController

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'fullName' => 'required|string|max:70',
        'email' => 'required|email|max:255',
        'phoneNumber' => 'nullable|regex:^(+d{1,2}s)?(?d{3})?[s.-]d{3}[s.-]d{4}$^',
        'message' => 'required|string|min:50',
    ]);

    if ($validator->fails()) {
        return back()
                    ->withErrors($validator)
                    ->withInput();
    }
    
    $contact = new Contact;
    $contact->fullName = $request->fullName;
    $contact->email = $request->email;
    $contact->phoneNumber = $request->phoneNumber;
    $contact->message = $request->message;
    $contact->save();
    
    return redirect('/contact')->with('status', 'Thanks for contacting me. I'll be in touch soon!');
}

Contact model

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Contact extends Model
{
    use HasFactory;

    protected $table = 'contacts';

    protected $fillable = ['fullName', 'email', 'phoneNumber', 'message'];
}

Migration file

public function up()
{
    Schema::create('contacts', function (Blueprint $table) {
        $table->id();
        $table->string('fullName');
        $table->string('email');
        $table->string('phoneNumber')->nullable;
        $table->text('message');
        $table->timestamps();
    });
}

Any idea why the first field “fullName” won’t accept anything? No matter what I type in, it says the required field is empty. Thanks.

Advertisement

Answer

You have a typo on your name field it is fullName not fullname

<div class="relative">
    <label for="text">Full Name</label>
    <input type="text" name="fullName" placeholder="John Doe" value="{{ old('fullName') }}" required />
    @error('fullName')
        <div>{{ $message }}</div>
    @enderror
</div>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement