How to Return Custom Error Message from Controller Method Validation

How to return custom error message from controller method validation

to get custom error message you need to pass custom error message on third parameter,like that

$this->validate(
$request,
['thing' => 'required'],
['thing.required' => 'this is my custom error message for required']
);

How can I set custom validation error messages from a controller, in Laravel 8?

Maybe this can work for ya.

    $rules = [
'first_name' => 'required|string,',
'last_name' => 'required|string',
'email' => 'required|string|unique:users,email',
'password' => 'required|string|confirmed',
'accept' => 'accepted'
];

$customMessages = [
'required' => 'The :attribute field is required.'
];

$this->validate($request, $rules, $customMessages);

Also check out the laravel customizing error messages documentation.

How can I manually return or throw a validation error/exception in Laravel?

As of laravel 5.5, the ValidationException class has a static method withMessages that you can use:

$error = \Illuminate\Validation\ValidationException::withMessages([
'field_name_1' => ['Validation Message #1'],
'field_name_2' => ['Validation Message #2'],
]);
throw $error;

I haven't tested this, but it should work.

Update

The message does not have to be wrapped in an array. You can also do:

use Illuminate\Validation\ValidationException;

throw ValidationException::withMessages(['field_name' => 'This value is incorrect']);

Spring Boot how to return custom error message when validating @PathVariable parameters

For your particular use case, you can use @ExceptionHandler in the Controller or in a @ControllerAdvice class as shown here. For example, I am returning NOT_FOUND error for the sake of it.

@ExceptionHandler({MethodArgumentTypeMismatchException.class})
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "this is the reason")
public void handle() {
}

You may not see the reason in the actual error response, until you enable

server:
error:
include-message: always

If you think your @ExceptionHandler is only needed in a Controller class you can keep the method inside the controller. Alternatively you can create a @ControllerAdvice class and put the method there, so that you can reuse across multiple controllers in your application.


However, if you want a more complex validation, I will suggest to keep the id type to String and then cast manually into Long and perform the validation. Doing so you can throw your own RuntimeException and handle different cases.

How to show only one error message from Laravel request rules

-For custom message


Laravel Documents customizing-the-error-messages

-For return single error message

    //In Request
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'success' => false,
'message' =>$validator->errors()->first(),
'data' => null,
], 422));
}

Laravel Validator - Get error messages as an array in controller

$validation = Validator::make($request->all(), [
'template_id' => 'required',
'email' => ['required', 'email', 'unique:user,email'],
'first_name' => 'required',
'last_name' => 'required',
]);

if you want all errors same time you just write code like

$validation->messages()->all()

or if you want one by one error so write code like

$validation->messages()->first()


Related Topics



Leave a reply



Submit