Laravel Validator::extendedDependent() 动态错误信息

Laravel Validator::extendedDependent() dynamic error message

Validator::extendDependent('cbu_deposit', function ($attribute, $value, $parameters, $validator){     
             $arr = explode('.', $attribute);
             $account = $validator->getData()[$arr[0]][$arr[1]];

             $type = $account['type'];
             if($value < $type['minimum_deposit_per_transaction']){
                return false;
             }
             return true;
});

根据laravel api第三个参数是$message

void extendDependent(string $rule, Closure|string $extension, string|null $message = null)

我想知道如何使他的错误消息像这样动态显示

$error = "Mininum deposit for " .$type['product_id']. ' is : '.$type['minimum_deposit_per_transaction'].'.';

您可以像这样向错误消息添加变量:

Mininum deposit for :id is : :deposit.

检查 https://laravel.com/docs/validation#custom-error-messages

我已经使用 Validator::extendDependent('cbu_deposit') 中的 $validator->addReplacer() 方法找到了答案。

 $customMessage = "Mininum deposit for " .$type['product_id']. ' is : '.$type['minimum_deposit_per_transaction'].'.';

            $validator->addReplacer('cbu_deposit', 
                function($message, $attribute, $rule, $parameters) use ($customMessage) {
                    return \str_replace(':custom_message', $customMessage, $message);
                }
            );