For example I have validation of HTML form:
$validator = Validator::make($request->all(), [
"code" => "required|string|size:10"]);
If I get error it look as:
The code field is required.
Wrere code is name of field.
How can I translate(localize) this?
You can pass 3rd argument in make method like this:
$rules = [
"code" => "required|string|size:10",
];
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
See more about Custom Validation Error Messages
Hop this helps!
Related
How can i override this text on laravel, when validation fails
I've searching this text all over project but didn't find also
If you have a min:8 rule for the password field, we can create a custom message.
e.g: $rules = [
'password' => 'min:8|required'
];
$messages = [
'password.min' => 'your custom message',
'password.required' => 'your custom message'
];
$isValid = Validator::make($requestInputs, $rules, $messages);
When using the validation like below with custom messages how is possible to check if the validation fails?
$rules = [
'email' => 'required|email'
];
$customMessages = [
'email.required' => 'The email is required.',
'email.email' => 'Please insert a valid email.'
];
$this->validate($request, $rules, $customMessages);
Like below, without custom messages is possible to use $validator->fails() but in the above case how to verify if the validation fails?
$validator = Validator::make($request->all(), [
//...
]);
if ($validator->fails()) {
//...
}
When you use $this->validate it validate your request against your validation rules and if failed then it redirect to previous page with validation message. If you want to use $validator->fails() then use Validator::make with custom message. Like this
$rules = [
'email' => 'required|email'
];
$customMessages = [
'email.required' => 'The email is required.',
'email.email' => 'Please insert a valid email.'
];
$validator = Validator::make($request->all(), $rules, $customMessages);
if ($validator->fails()) {
//...
}
Check validator make method https://laravel.com/api/5.0/Illuminate/Validation/Factory.html#method_make
much simpler solution is to use custom handler in app\Exceptions\Handler.php:
public function register()
{
$this->renderable(function (ValidationException $e, $request) {
//list of specific failed tests
$e->validator->failed();
return response()->json(['message' => 'custom message','meow'=>'meow',$e->status);
});
}
this way you keep old behavior AND can still change messages sent back to the user/frontend
$id = $request->id;
$validation = Validator::make($request->all(), [
'email' => 'unique:customers,email,'.$request->id
]);
You're using a custom validator. You need to handle the validation failure manually. Also your code checks for unique email in the table customers except for the email of the user $request->id. I assume this is intended.
$validator = \Validator::make($request->all(), [
'email' => 'email|unique:customers,email,' . $request->id
]);
if ($validator->fails()) {
// Handle failure
}
The code below will automatically handle validation failure and redirect back with errors and inputs.
$this->validate($request, [
'email' => 'email|unique:customers,email,' . $request->id
]);
You can try something like
Validator::make($data, [
'email' => [
'required',
Rule::unique('customers')->ignore($customer->id),
],
]);
How can I change the value used in the error feedback, lets say I have this rule:
$rules = array(
'valid_country_code' => 'required',
);
But instead of 'valid_country_code' I want the user to see 'country' in the error message. The message at the moment.
valid_country_code is required.
what I want
country is required.
But I dont want to change the name in the form when posting because I want to bind the form to a model.
You may pass the custom messages as the third argument to the Validator::make method:
$rules = array(
'valid_country_code' => 'required',
);
$messages = [
'valid_country_code.required' => 'country is required.',
];
$validator = Validator::make($input, $rules, $messages);
I am using following code in controller file. I have an element name title in view form. Following code is showing correct validation. But, i need to customize this error.
$v = Validator::make($request->all(), [
'title' => 'required|max:255',
]);
if ($v->fails())
{
return redirect()->back()->withErrors($v->errors());
}
You can pass as third argument of Validator your custom messages.
$messages = [
'title.required' => 'You need to insert a pretty title'
];
v = Validator::make($request->all(), [
'title' => 'required|max:255',
], $messages
);