Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

the_lar's avatar

Add DoB validation on UpdateUserProfile

Hi, I've added date of birth fields to User profile in a Laravel 9 Fortify/Livewire app and just want to add something sensible to check it's valid. DoB can also be null. Here's my current UpdateUserProfileInformation.php file:

<?php

namespace App\Actions\Fortify;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;

class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
    /**
     * Validate and update the given user's profile information.
     *
     * @param  mixed  $user
     * @param  array  $input
     * @return void
     */
    public function update($user, array $input)
    {
        Validator::make($input, [
            'firstname' => ['required', 'string', 'max:255'],
            'middlename' => ['nullable', 'string', 'max:255'],
            'lastname' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
            'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
        ])->validateWithBag('updateProfileInformation');

        if (isset($input['photo'])) {
            $user->updateProfilePhoto($input['photo']);
        }

        if ($input['email'] !== $user->email &&
            $user instanceof MustVerifyEmail) {
            $this->updateVerifiedUser($user, $input);
        } else {
            $user->forceFill([
                'firstname' => $input['firstname'],
                'middlename' => $input['middlename'],
                'lastname' => $input['lastname'],
                'email' => $input['email'],
            ])->save();
        }
    }

    /**
     * Update the given verified user's profile information.
     *
     * @param  mixed  $user
     * @param  array  $input
     * @return void
     */
    protected function updateVerifiedUser($user, array $input)
    {
        $user->forceFill([
            'firstname' => $input['firstname'],
            'middlename' => $input['middlename'],
            'lastname' => $input['lastname'],
            'email' => $input['email'],
            'email_verified_at' => null,
        ])->save();

        $user->sendEmailVerificationNotification();
    }
}

My 3 DoB input fiels are Day (dob_day), Month (dob_month) and Year (dob_year) - I'm already doing some checking with JS to ensure that sensible values are entered - e.g. day must be a number and less than or equal to 31.

I think it's reasonable to allow date of birth where age would be between 18 and 100 - I'm comfortable using nabive PHP DateTime Class to do this but is there a more laravel way of doing it and how would I incorporate into my file above?

0 likes
5 replies
jlrdw's avatar

I suggest a regular date to verify instead of separate fields. Just suggestion. .

jlrdw's avatar

@the_lar UX has nothing to do with it, in code put it back together as a date and validate it. User has nothing to do with the controller code you program.

the_lar's avatar

@jlrdw oh OK, yes that's what I intend to do, my question was just about what the best way to do that is in the file I showed and how to structure the rules to check that the age is not more than 100

Please or to participate in this conversation.