Help us understand the problem. What is going on with this article?

【Laravel】エラーメッセージ、値部分のカスタマイズ

More than 1 year has passed since last update.

やりたいこと

required_if等で 「◯◯が▲▲の場合、□□も指定してください」で、▲▲部分が値で直接表示されるのでカスタムしたい。
ただし、Validatorファサードでカスタムはしたくない。

想定

userテーブルにroleカラムがあり、roleが1(承認者)の場合は承認対象ページも入力させたい。

コード

nameにprefixでテーブル名をつけいている場合。

app/Http/Requests/UserRequest.php
    public function rules()
    {
        return [
            'user.role' => 'required',
            'user.authority_target' => 'required_if:user.role,1',
        ];
    }
resources/lang/ja/validation.php
<?php

return [

// 〜省略〜

    'values' => [
        'user' => [
            'role' => [
                '1' => '承認者',
            ]
        ],
        // 下記だと読み込まれない
        //'user.role' => [
        //  '1' => '承認者',
        //],
    ]
];


蛇足

上記の設定ファイル読み込んでいる部分

vendor/laravel/framework/src/Illuminate/Validation/Concerns/FormatsMessages.php
    // 304行目〜
    /**
     * Get the displayable name of the value.
     *
     * @param  string  $attribute
     * @param  mixed   $value
     * @return string
     */
    public function getDisplayableValue($attribute, $value)
    {
        if (isset($this->customValues[$attribute][$value])) {
            return $this->customValues[$attribute][$value];
        }

        $key = "validation.values.{$attribute}.{$value}";

        if (($line = $this->translator->trans($key)) !== $key) {
            return $line;
        }

        return $value;
    }

間違いや良き方法ありましたら、コメントください。
以上。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away