Ad

Display Only One Validation Error Message

- 1 answer

There is a validation rule for a model:

 $rules = [
     'uid' => 'required|digits_between:3,9|numeric'
 ];

 $messages = [
     'uid.digits_between' => 'Length between 3 and 9',
     'uid.numeric' => 'Only numbers',
     '*.required' => 'The field is required'
 ];

If I type a character in uid input 2 messages will display - Length between 3 and 9 and Only numbers.

How to display only one message in this situation - Only numbers, but keep digits_between rule working.

Ad

Answer

Did you try bail rule? (https://laravel.com/docs/5.8/validation#quick-writing-the-validation-logic - Stopping On First Validation Failure)

$rules = [
     'uid' => 'bail|required|digits_between:3,9|numeric'
];
Ad
source: stackoverflow.com
Ad