Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial translation with validation rule required_if #5866

Open
toto975 opened this issue Jun 19, 2024 · 0 comments
Open

Partial translation with validation rule required_if #5866

toto975 opened this issue Jun 19, 2024 · 0 comments

Comments

@toto975
Copy link
Contributor

toto975 commented Jun 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant