Search code examples
phplaravellaravel-jetstream

Customize Jetstream Registration Page Validation Error Message in Laravel 8


I am using the default jetstream registration page in laravel 8:

 @if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
  @endif


          <div class="container d-flex flex-column justify-content-between vh-100">
          <div class="row justify-content-center mt-5">
            <div class="col-xl-5 col-lg-6 col-md-10">
              <div class="card">
                <div class="card-header bg-primary">
                  <div class="app-brand">
                    <a href="/index.html">
                      <svg class="brand-icon" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid" width="30"
                        height="33" viewBox="0 0 30 33">
                        <g fill="none" fill-rule="evenodd">
                          <path class="logo-fill-blue" fill="#7DBCFF" d="M0 4v25l8 4V0zM22 4v25l8 4V0z" />
                          <path class="logo-fill-white" fill="#FFF" d="M11 4v25l8 4V0z" />
                        </g>
                      </svg>
                      <span class="brand-name">Sleek Dashboard</span>
                    </a>
                  </div>
                </div>
                <div class="card-body p-5">
                  <h4 class="text-dark mb-5">Sign Up</h4>
                  <form method="POST" action="{{ route('register') }}">
                    @csrf
        
                    <div class="row">
                      <div class="form-group col-md-12 mb-4">
                        <input type="text" name="name" :value="old('name')" required autofocus autocomplete="name" class="form-control input-lg" id="name" aria-describedby="nameHelp" placeholder="Name">
                        
                        @if ($errors->has('name'))
                        <span class="invalid-feedback">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                       @endif


                      </div>
                      <div class="form-group col-md-12 mb-4">
                        <input type="email" name="email" :value="old('email')" required class="form-control input-lg" id="email" aria-describedby="emailHelp" placeholder="Email">
                      </div>
                      <div class="form-group col-md-12 ">
                        <input type="password" name="password" required autocomplete="new-password" class="form-control input-lg" id="password" placeholder="Password">
                      </div>
                      <div class="form-group col-md-12 ">
                        <input type="password" name="password_confirmation" required autocomplete="new-password" class="form-control input-lg" id="cpassword" placeholder="Confirm Password">
                      </div>
                      <div class="col-md-12">

                        <input type="checkbox" name="terms" value="true" {{ !old('terms') ?: 'checked' }}>
                        <label>Do you agree to Terms & Conditions</label><br/><br/>


                        <button type="submit" class="btn btn-lg btn-primary btn-block mb-4">Sign Up</button>
                        <p>Already have an account?
                          <a class="text-blue" href="{{ route('login') }}">Sign in</a>
                        </p>
                      </div>
                    </div>
                  </form>

                </div>
              </div>
            </div>
          </div>

I was wondering how can I customize these error messages when the validation measure occurs?

enter image description here

Which page should I edit? I went through config/auth.php but I did not see anything there to edit.

Also visited the model and there is nothing to check there.

Please help!


Solution

  • If you are looking for custom validation error messages then you can change it in App\Http\Controllers\Auth\RegisterController

    protected function validator(array $data)
        {
            return Validator::make($data, [
                'username' => ['required', 'string', 'max:255', 'unique:users'],
                'first_name' => ['required', 'string', 'max:255'],
                'last_name' => ['required', 'string', 'max:255'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
                'password' => ['required', 'string', 'min:6', 'confirmed'],
            ],[
                'username.required'=>'custom message',
                'username.string'=>'custom message',
                'username.max'=>'custom message',
                'username.unique'=>'custom message',
            ]);
        }
    

    App\Actions\Fortify\CreateNewUser

    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'] : '',
        ],,[
            'name.required'=>'Name is required',
            'email.required'=>'Email is required',
            'phone.required'=>'Phone is required',
        ])->validate();
    
            return User::create([
                'name' => $input['name'],
                'email' => $input['email'],
                'password' => Hash::make($input['password']),
            ]);
        }