Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I add custom validation rules to the CreateNewUser class


How can I add custom validation rules to the CreateNewUser class


To add custom validation rules to the `CreateNewUser` class in Laravel Fortify, you can follow these steps:

1. Create a new file called `CreateNewUser.php` in your `app/Actions/Fortify` directory. This file should contain your customized implementation of the `CreateNewUser` class.

php
<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    /**
     * Validate and create a newly registered user.
     *
     * @param  array  $input
     * @return \App\Models\User
     */
    public function create(array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),
            'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
            'custom_field' => ['required', 'string', 'max:100'], // Add your custom validation rule
        ])->validate();

        return User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
            'custom_field' => $input['custom_field'], // Save the custom field
        ]);
    }
}

2. In your customized `CreateNewUser` class, you can add your custom validation rules to the `Validator::make` method. In the example above, we added a custom field called `custom_field` with the following validation rules:
- `required`: The field is required.
- `string`: The field must be a string.
- `max:100`: The maximum length of the field is 100 characters.

3. If you need to save the custom field in the user model, you can add it to the `$fillable` property of your `User` model.

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'custom_field', // Add the custom field to the fillable attributes
    ];

    // ...
}

4. In your application, use the customized `CreateNewUser` class instead of the default one provided by Fortify.

php
use App\Actions\Fortify\CreateNewUser;

$user = app(CreateNewUser::class)->create($input);

By following these steps, you can add custom validation rules to the `CreateNewUser` class in Laravel Fortify. The custom field will be validated according to the specified rules and saved in the user model if the validation passes.