PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, April 18, 2022

[FIXED] Why Laravel error messages are not correct in my app ? = ex: validation.min.string

 April 18, 2022     authentication, breeze, laravel     No comments   

Issue

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 class="validation-error">{{ $message }}</span>
@enderror
@error('password')
       <span class="validation-error">{{ $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 ?


Solution

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



Answered By - Omar Tammam
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing