Custom Laravel validation messages
If you use $this->validate()
simplest one, then you should write code something like this..
$rules = [
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:250',
];
$customMessages = [
'required' => 'The :attribute field is required.'
];
$this->validate($request, $rules, $customMessages);
How to custom message validator with Laravel
On the Request php file you can use this function failedValidation() and pass in a Validator. This way you can alter or customize the response if validation fails.
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
protected function failedValidation(Validator $validator) {
throw new HttpResponseException(response()->json(['status'=>'failed',
'message'=>'unprocessable entity',
'errors'=>$validator->errors()->all()], 422));
}
Sample response is here..
{
"status": "failed",
"message": "unprocessable entity",
"errors": [
"The name must be a string.",
"One or more users is required"
]
}
As you can see the message is changed now you can do whatever you want on the response message.
Also you can try this
$validator->errors()->messages()[keyname]
custom validation message in laravel 8
You can use custom validator. Check the documentation. You can get messages from $errors variable in blade template.
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
], [
'title.required' => 'Title required custom message',
'title.unique' => 'Title unique custom message',
// ...
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
If you want to send redirect with data. You can use
redirect()->route('route')->with('data', 'test');
You can get after redirect
session('data')
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 to set laravel custom validation message with laravel rules
You may customize the error messages
used by the form request
by overriding the messages()
method. Add custom messages
on your Venue
class as follows-
public static function messages($id = '') {
return [
'name.required' => 'You must enter your name',
'logo.required' => 'You must upload logo',
'key.rules' => 'your messages'
];
And on your controller add messages
as third parameter
like-
$this->validate($request, Venue::rules(), Venue::messages());
Related Topics
Loading View Outside View Folder with Codeigniter
Regex Matching Links Without <A> Tag
Display Possible Combinations of String
What Is the Use of '\G' Anchor in Regex
Authentication on Google: Oauth2 Keeps Returning 'Invalid_Grant'
How to Destroy the Session Cookie Correctly with PHP
Unset a Column in a Multidimensional Array
Scroll Bar for Datalist in HTML5
Pg_Query Result Contains Strings Instead of Integer, Numeric
How to Send Email with Attachment Using PHP
Passing JavaScript Variable to PHP Using Ajax
Query_Posts() Should Be Avoided
Concatenating a String and Primary Key Id While Inserting