Support the ongoing development of Laravel.io →
Validation

As requested by Mr. Mohammad Said on Github , I am writing my question here.... :)

  • Laravel Version: 5.3.16
  • PHP Version: 5.6.26
  • Database Driver & Version: 5.7.15

Issue 1:

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.

Issue 2:

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, :)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.