thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.72k stars 2.67k forks source link

Partial translation with validation rule required_if #5866

Open toto975 opened 1 week ago

toto975 commented 1 week ago

Laravel version

10.48.12

PHP version

8.3.3

Voyager version

1.7

Database

MariaDB 13.2

Description

I see a partial translation when using a validation rule required_if

Steps to reproduce

In the Bread, I use this validation rule for "unite_id" field :

    "validation": {
        "rule": "required_if:utilisateurs,null",
        "messages": {
            "required_if": "unité ou utilisateurs est obligatoire."
        }
    },

image

I use this validation rule for "Contributeurs" relation field :

    "validation": {
        "rule": "nullable|required_if:unite_id,null",
        "messages": {
            "required_if": "L'un des 2, Unité ou Contibuteurs, est obligatoire."
        }
    }

image

When i add datas, the message displayed is Le champ Contributeurs 1 est obligatoire quand la valeur de Unité responsable est empty. 2 problems :

  1. "empty" is not translated.
  2. it's not my custom message which is displayed
  3. Only one error message is displayed.

Do you think it's Laravel problem or tcg Voyager problems ?

For the first problem, in vendor\laravel\framework\src\Illuminate\Validation\Concerns\ReplacesAttributes.php, i add $this->translator->get()

    protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
    {
        $parameters[1] = $this->translator->get($this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0])));

        $parameters[0] = $this->getDisplayableAttribute($parameters[0]);

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

For vendor\laravel\framework\src\Illuminate\Translation\Translator.php, i dump $this->loaded

    public function get($key, array $replace = [], $locale = null, $fallback = true)
    {
        $locale = $locale ?: $this->locale;

        // For JSON translations, there is only one file per locale, so we will simply load
        // that file and then we will be ready to check the array for the key. These are
        // only one level deep so we do not need to do any fancy searching through it.
        $this->load('*', '*', $locale);

        $line = $this->loaded['*']['*'][$locale][$key] ?? null;
dump($line);
dump($this->loaded);

I see that in $this->loaded[' '], there is two value "" and "validation" :

array:1 [▼ // vendor\laravel\framework\src\Illuminate\Translation\Translator.php:152
  "*" => array:2 [▼
    "*" => array:1 [▶]
    "validation" => array:2 [▼
      "fr" => array:76 [▶]
      "en" => array:100 [▶]
    ]
  ]
]

For vendor\laravel\framework\src\Illuminate\Translation\Translator.php, when i change $line = $this->loaded['*']['*'][$locale][$key] ?? null; by $line = $this->loaded['*']['validation'][$locale][$key] ?? null; empty is well translated, the message is Le champ utilisateurs est obligatoire quand la valeur de Unité responsable est vide.

So, Laravel problem or tcg Voyager problems ?

Expected behavior

Two errors message should be displayed. :

  1. My custom message for "unité"
  2. My custom message for "Contributeurs"

Screenshots

No response

Additional context

No response