Home > Back-end >  Why Laravel error messages are not correct in my app ? = ex: validation.min.string
Why Laravel error messages are not correct in my app ? = ex: validation.min.string

Time:04-13

I have a weird problem today with my laravel app : Validation error messages are not the same as usual : for example "password must be at least 8 caracters". Instead of this, the error message displays "validation.min.string".

I never had this sort of problems, and don't understand what happen.

I tried adding messages() method in the LoginRequest (I am using Breeze for authentification) :

class LoginRequest extends FormRequest
{
 
 
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'email' => 'required|string|email',
        'password' => 'required|string|min:6',
    ];
}

public function messages()
{
    return [
        'email.required' => 'Email is required',
        'email.email' => 'Email is invalid',
        'password.required' => 'Password is required',
        'password.min:6' => 'Password must be at least 6 characters',
    ];
}

/**
 * Attempt to authenticate the request's credentials.
 *
 * @return void
 *
 * @throws \Illuminate\Validation\ValidationException
 */
public function authenticate()
{
    $this->ensureIsNotRateLimited();

    if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
        RateLimiter::hit($this->throttleKey());

        throw ValidationException::withMessages([
            'email' => trans('auth.failed'),
        ]);
    }

    RateLimiter::clear($this->throttleKey());
}

/**
 * Ensure the login request is not rate limited.
 *
 * @return void
 *
 * @throws \Illuminate\Validation\ValidationException
 */
public function ensureIsNotRateLimited()
{
    if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
        return;
    }

    event(new Lockout($this));

    $seconds = RateLimiter::availableIn($this->throttleKey());

    throw ValidationException::withMessages([
        'email' => trans('auth.throttle', [
            'seconds' => $seconds,
            'minutes' => ceil($seconds / 60),
        ]),
    ]);
}

/**
 * Get the rate limiting throttle key for the request.
 *
 * @return string
 */
public function throttleKey()
{
    return Str::lower($this->input('email')).'|'.$this->ip();
}
}

Of course I display the messages in my view, using blade directives :

@error('email')
       <span >{{ $message }}</span>
@enderror
@error('password')
       <span >{{ $message }}</span>
@enderror

But nothing more happens... just the same unfriendly message "validation.min.string" or "auth.failed" or something like this depending the error.

Authentication works well, it's just a problem with those error messages.

I had the same problem with an other form and i solved it creating a Request with rules and messages methods. This time it doesn't work.

Anyone could help me to solve it ?

CodePudding user response:

This issue is in the custom validation message this index password.min:6 need to change to password.min

for more info please check Laravel custom error message

CodePudding user response:

I suggest you use this code:

public function messages(): array
{
    return [
        'email.required' => 'Email is required',
        'email.email' => 'Email is invalid',
        'password.required' => 'Password is required',
        'password.min' => 'Password must be at least 6 characters',
    ];
}
  • Related