As requested by Mr. Mohammad Said on Github , I am writing my question here.... :)
So, I have been trying to make a custom validation rule in cases like require_with_not_empty
. Laravel's require_with
method checks only if the field inside is present or not, but I also need to check if the field is present and not empty.
In cases like -
$rules = [
'post.*.description' => 'required_with_not_empty:post.*.name,post.*.date|min:1|max:2000',
];
I am adding this to my ValidationServiceProvider
Validator::extend('required_with_not_empty', function($attribute, $value, $parameters, $validator) {
dd($attribute, $parameters);
});
Output in this case is
"post.0.description"
array => [
0 => "post.*.name"
]
The main question here is... how is it possible to convert post.*.name
to post.0.name
?
We can get the value of the attribute by doing
array_get($validator->getData(), $attribute, null)
but getting the value of parameter here doesn't seem possible.
If we go into resources -> lang -> validation.php
, We get an option to add custom messages for custom attributes.
So, here my custom attribute is required_with_not_empty
. And my current array looks like
'custom' => [
'education.*.description' => [
'required_with_not_empty' => 'The :attribute field must be present and not empty with :values.',
],
],
But what I get as an output is
The post descriptions must be present and not empty with :values
Here... description for some reason is given as plural and :values is not getting translated to the parameters provided in the validation rule.
Before, commenting below please read Mr. Said's and my response here
Thanks, :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community