简体   繁体   中英

After a (validation) error occurs in the backend (Laravel Jetstream), how to return back to register page showing the error as status?

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']
        ]);
    }
}

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM