Use custom localized validation message with object rule - validation

Problem
According to Laravel docs here, i can add in the custom section of my validation.php file my custom validation error messages, but how should i specify the rule for which the message is for if the rule in my request is an object rule?
request
return [
'parent.field' => ['nullable', new ObjcetRule()],
];
validations.php
return [
// other messages
'custom' => [
'field' => [
'nullable => 'nullable rule custom message',
'???' => 'object rule custom message', // what key should i use here?
],
],
];
My specific case
I'm using BenSampo/laravel-enum package for enums, but in the validations section of its doc there is nothing about that.

Related

FormRequest messages() function does not translate all rules [duplicate]

I'm working on a Laravel 5.8 project and trying to show custom validation messages for a validation which uses the requiredIf validation rule.
Here is how I have it set up:
$validation = Validator::make(
$request->all(),
[
...
'sum' => [
Rule::requiredIf(function() use ($request){
$model = Model::find($request->id);
return $model->is_special; //returns a boolean value
}),
'numeric'
],
...
],
[
...
'sum.required_if' => 'This cannot be blank',
'sum.numeric' => 'Must use a number here',
...
]
);
Now the validation is working correctly and the custom message for the numeric validation shows as should, but the message I get for the requiredIf() method is Laravel's default error message.
I also tried using 'sum.requiredIf' => '...' but that didn't work either and can't seem to find any documentation or example for this scenario.
I was tinkering with this for a while and noticed that for this to work I needed to define
'sum.required' => 'This cannot be blank'
and not 'sum.required_if' => 'This cannot be blank',.
Not sure if this is expected behavior or just a workaround but my deduction is that with the callback Rule::requiredIf(function() use ($request){...}) the parameters :other and :value are not passed so it falls back onto required messaging and I guess this makes sense since required_if and required would not be used on the same :attribute.
Hope this helps anyone who comes across this problem.
First, create a rule name isSpecial or whatever
php artisan make:rule isSpecial
Go to App\Rules\isSpecial.php
private $id;
public function __construct($id) // pass id or what you need
{
//
$this->id=$id;
}
public function passes($attribute, $value) // customize your rules here
{
//
return Model::find($request->id)->is_special;
}
public function message() // here is answer for your question
{
return 'The validation error message.'; // your message
}
in your controller
use App\Rules\isSpecial;
\Validator::make($request->all(), [
'sum' => new isSpecial() ,
])->validate();
another idea :
Specifying Custom Messages In Language Files
In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.
'custom' => [
'email' => [
'required' => 'We need to know your e-mail address!',
],
],
Simple notice:
- I suggest using HTTP Requests instead use validation in your controller and function direct
Looks like as of Laravel 8, using required_if works as expected, and alternatively will not fall back on required as mentioned previously:
'sum.required_if' => 'This cannot be blank',

Custom error message for `requiredIf` validation in laravel

I'm working on a Laravel 5.8 project and trying to show custom validation messages for a validation which uses the requiredIf validation rule.
Here is how I have it set up:
$validation = Validator::make(
$request->all(),
[
...
'sum' => [
Rule::requiredIf(function() use ($request){
$model = Model::find($request->id);
return $model->is_special; //returns a boolean value
}),
'numeric'
],
...
],
[
...
'sum.required_if' => 'This cannot be blank',
'sum.numeric' => 'Must use a number here',
...
]
);
Now the validation is working correctly and the custom message for the numeric validation shows as should, but the message I get for the requiredIf() method is Laravel's default error message.
I also tried using 'sum.requiredIf' => '...' but that didn't work either and can't seem to find any documentation or example for this scenario.
I was tinkering with this for a while and noticed that for this to work I needed to define
'sum.required' => 'This cannot be blank'
and not 'sum.required_if' => 'This cannot be blank',.
Not sure if this is expected behavior or just a workaround but my deduction is that with the callback Rule::requiredIf(function() use ($request){...}) the parameters :other and :value are not passed so it falls back onto required messaging and I guess this makes sense since required_if and required would not be used on the same :attribute.
Hope this helps anyone who comes across this problem.
First, create a rule name isSpecial or whatever
php artisan make:rule isSpecial
Go to App\Rules\isSpecial.php
private $id;
public function __construct($id) // pass id or what you need
{
//
$this->id=$id;
}
public function passes($attribute, $value) // customize your rules here
{
//
return Model::find($request->id)->is_special;
}
public function message() // here is answer for your question
{
return 'The validation error message.'; // your message
}
in your controller
use App\Rules\isSpecial;
\Validator::make($request->all(), [
'sum' => new isSpecial() ,
])->validate();
another idea :
Specifying Custom Messages In Language Files
In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.
'custom' => [
'email' => [
'required' => 'We need to know your e-mail address!',
],
],
Simple notice:
- I suggest using HTTP Requests instead use validation in your controller and function direct
Looks like as of Laravel 8, using required_if works as expected, and alternatively will not fall back on required as mentioned previously:
'sum.required_if' => 'This cannot be blank',

How to use custom translations for requests in Laravel?

Documentation says to create directory with file by path: resources/lang/xx/validation.php.
Then add content of validation words:
return ['custom' => [
'email' => [
'required' => 'We need to know your e-mail address!',
],
]];
How to use this for Creating Form Requests where is going validation?
This is just for deploying custom validation messages, e.g. whenever 'email' is 'required' it will return this message in place of the default message: The email field is required.
If you want to replace all the other messages take a look at /resources/lang/en/validation.php here and see all the basic messages which you can replace with your local language versions in your /resources/lang/xx/validation.php
If you want a custom message, find the custom array at line 130 and change to:
'custom' => [
'price' => [
'required' => 'The price is required! Please supply one.',
],
],
Then, be sure to set your app locale, e.g.
Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale);
//
});
Or if your entire app is in another language, you could set locale in your /config/app.php on line 81.
Your usual validator will now use the messages in /resources/lang/locale/validation.php

Laravel Validation custom messages using nested attributes

I would like to ask for some advices about Laravel Validation...
Let's say I've got an input named invoiceAddress[name] and in a controller I've got a rule
$rule = ['invoiceAddress.name' => 'required',];
or just a
$validator = \Validator::make($request->all(), [
'invoiceAddress.name' => 'required',
]);
now, inside custom validation language file validation.php, am I able to nest attributes somehow? like:
'required' => ':attribute is mandatory',
'attributes' => [
'invoiceAddress' => [
'name' => 'blahblah'
],
],
If I try to nest the attribute the way above, i get
ErrorException
mb_strtoupper() expects parameter 1 to be string, array given
because I am using a field (as above)
['name' => 'blahblah']
I am trying to get custom messages using the file and the :attribute directive (as mentioned in the code above).
I am basically trying to do this How to set custom attribute labels for nested inputs in Laravel but i get the mentioned error...
Thank you in advance...
A Note On Nested Attributes
If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'author.name' => 'required',
'author.description' => 'required',
]);
Reference: https://laravel.com/docs/5.3/validation#quick-writing-the-validation-logic (A Note On Nested Attributes Section)

Laravel Custom Validation for multi-language application

I am using Laravel Standard Validation, but I want to customize it because I am using multi language in my application, so I have to customize the message.
$rule_validation = [
'phone' => 'required|max:20|min:6|regex:/^[0-9]+$/',
'agree_promo_code' => request('promo_code') ? 'accepted' : '',
'terms_of_services' => 'accepted',
'aware' => 'accepted'
];
Now I want to write customize validation message for agree_promo_code
I know to write message for phone, but having doubt in agree_promo_code, terms_of_services and aware.
Can anyone help me out to resolve this issue
But please keep in mind about the language. Thank you
You can pass second array to your validate function like
$this->validate($request,[
'phone' => 'required|max:20|min:6|regex:/^[0-9]+$/',
'agree_promo_code' => request('promo_code') ? 'accepted' : '',
'terms_of_services' => 'accepted',
'aware' => 'accepted'
],[
'phone.required' => trans('validation.phone'),
'agree_promo_code.accepted' => trans('validation.agree_promo_code'),
'terms_of_services.accepted' => trans('validation.terms_of_services'),
'aware.accepted' => trans('validation.aware'),
]);
and inside your resources/lang/{lang}/validation.php file ({lang} is your language directory).
you can do something like
return [
'phone' => 'Phone validation message',
'agree_promo_code' => 'agree_promo_code validation message',
'terms_of_services' => 'terms_of_services validation message',
];
So it will set your messages according to respective language.
As you set your Language files for respective language
you need to create validation.php file under respective language folder
and add custom message for each field for each type of validation applied
for example if you have name field and validation check is required
so in your language folder's validation file you will write as below
<?php
return [
'custom' => [
'name' => [
'required' => 'O campo nome é obrigatório.'
]
];
rest all language check will be handled by Laravel itself while displaying validation error messages as per your locale language selected for session

Resources