Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

franciscocaldeira's avatar

Laravel translate array with :position on rule required_if

I have rules() like this:

'lines.*.type' => ['required'],
'lines.*.text' => ['required_if:lines.*.type,1,2],

This outputs for example:

The field lines.0.text field is required when lines.0.type is 1.

But since I have in validation.php in attribute the values:

'lines.*.text' => 'text line #:position',
'lines.*.type' => 'type line #:position',

The output is:

The field text line #1 is required when type line #:position is 1.

(the :other don't do the :position).

I would do it as custom:

'custom' => [
    'lines.*.text' => [
        'required_if' => 'Each field text line is required when type line field is :value.',
    ],
],

"Each field text line field is required when field type line is 1."

but the client wants the exact key position:

The field text line #1 is required when field type line #1 is 1.

How can I do it?

0 likes
6 replies
Nakov's avatar

Try this:

AppServiceProvider.php

use Illuminate\Support\Facades\Validator;

public function boot()
{
      Validator::replacer('required_if', function ($message, $attribute, $rule, $parameters) {
           return str_replace([':position', ':value'], $parameters, $message);
       });
}

in the validation.php

'custom' => [
        'lines.*.text' => [
            'required_if' => 'The field text line #:position is required when field type line #:position is :value.',
        ],
    ],

'attributes' => [
        'lines.*.text' => ':position',
        'lines.*.type' => ':position',
    ],
franciscocaldeira's avatar

@Nakov I am having this:

BadMethodCallException: Method Illuminate\Validation\Validator::validateFormat does not exist. in file /iasaude/vendor/laravel/framework/src/Illuminate/Validation/Validator.php on line 1638\n

I have :

  Laravel Version .... 11.13.0  
  PHP Version ... 8.3.3-1+ubuntu20.04.1+deb.sury.org+1  
  Composer Version... 2.7.6  
Nakov's avatar

@franciscocaldeira you are missing the correct import, I wrote it above:

use Illuminate\Support\Facades\Validator;

so put that in the top of the class.

franciscocaldeira's avatar

@Nakov sorry, it fixes the :other with a :position and :second-position, etc with the correct array key. But now I need to do a custom for every required_if rule that I have, even without the :other having a :position because it makes like:

The field ABCD required when :other is 1.

Nakov's avatar
Nakov
Best Answer
Level 73

@franciscocaldeira you can check if it contains :position then..

Validator::replacer('required_if', function ($message, $attribute, $rule, $parameters) {

            if (str($message)->contains(':position')) {
                return str_replace([':position', ':value'], $parameters, $message);
            }

            return str_replace([':other', ':value'], $parameters, $message);
        });
franciscocaldeira's avatar

@Nakov other problems have occurred, the translation in :other was not replaced. Probably :values translation will fail too..

This is so far what I've done to try to fix it:

Validator::replacer('required_if', function ($message, $attribute, $rule, $parameters) {
  if (str($message)->contains(':position')) {
    return str_replace([':position', ':value'], $parameters, $message);
  }

  // replace the key number to * for localization
  $parameters[0] = trans('validation.attributes')[preg_replace('/(?<=\.)\d+/', '*', $parameters[0])];
  return str_replace([':other', ':value'], $parameters, $message);
});

Please or to participate in this conversation.