Home > database >  After a (validation) error occurs in the backend (Laravel Jetstream), how to return back to register
After a (validation) error occurs in the backend (Laravel Jetstream), how to return back to register

Time:12-26

ErrorImage

I have added phone number coloumn in jetstream register and make it unique. Therefore, there wont be same phone number in the user table.

I tried to test it with registering the same phone number, and the error came. So its working. But how do i like return back to register page and put a status there instead of Laravel Error Page.


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() ? ['accepted', 'required'] : '',
        ])->validate();

        return User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
            'phonenum' => $input['phonenum']
        ]);
    }
}

CodePudding user response:

According to the code snippet you've shared. The validator is validating other properties but not the phone number. So add the phone number field and it should redirect back with an error property. You will access the error property as you do with every other property.

        Validator::make($input, [
            'phonenum' => 'required,unique:users',
            'name' => ['required', 'string', 'max:255'],
             ....
        ])->validate();
  • Related