日期验证 - 如何 localize/translate 字符串 "today" 和 "tomorrow"

Date validation - How localize/translate strings "today" and "tomorrow"

在我的模型中,我使用 beforeafter:

为日期字段定义了一些验证规则
'birth_date' => 'required|date|before:today|after:01-jan-1920',
'another_date' => 'required|date|before:tomorrow|after:01-jan-1990',

验证工作正常,但我不知道如何翻译验证消息中的字符串 todaytomorrow

validation.php 语言文件中,afterbefore 消息是可本地化的,但是消息的 :date 部分仍然显示 [= 的英文版本17=] 和 tomorrow.

"after"            => "The :attribute must be a date after :date.",
"before"           => "The :attribute must be a date before :date.",

如何在验证消息中本地化这两个词 - todaytomorrow

进行一些自定义验证可能更有意义,但我认为您应该能够简单地使用 Carbon 来完成此操作:

$dt = new Carbon\Carbon();
$today = $dt->today();
$tomorrow = $dt->tomorrow();

    $rules = [
        ...
        'birth_date' => 'required|date|before:'.$today.'|after:01-jan-1920',
        'another_date' => 'required|date|before:'.$tomorrow.'|after:01-jan-1990'
    ];

您可以在验证语言文件或代码本身中为每个字段使用自定义验证消息:https://laravel.com/docs/5.2/validation#custom-error-messages

让我们模拟一个控制器验证,看看它是如何工作的:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class YourController extends Controller
{
    public function store(Request $request)
    {
        $rules = [
            'birth_date' => 'required|date|before:today|after:01-jan-1920',
        ];
        $messages = [
            'birth_date.before' => 'A data de nascimento deve ser uma data antes de hoje.', // "The birth date must be a date before today" in portuguese
        ];

        $this->validate($request, $rules, $messages);

        /* your stuff */
    }
}

您也可以使用 form requests(更好),您需要做的就是 return 在 messages() 方法中自定义翻译的消息。 :)

使用自定义错误消息。

$this->validate(
            $request,
            [
                'phone' => 'required|regex:/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i'
            ],
            [
                'regex' => 'You must enter a valid phone number.',
                'required' => 'You must enter a valid phone number.'
            ]
        );

将 'regex' 和 'required' 替换为 'before:today' 和 'before:tomorrow',并替换为自定义错误消息。

简而言之,将以下代码添加到resources/lang/whichever/validation.php

'values' => [
    // or whatever fields you wanna translate
    'birth_date' => [
        // or tomorrow
        'today' => '今天'
    ]
]

解释:

https://github.com/laravel/framework/blob/7.x/src/Illuminate/Validation/Concerns/FormatsMessages.php#L319

/**
 * 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];
    }

    // the key we want
    $key = "validation.values.{$attribute}.{$value}";

    // if the translate found, then use it
    if (($line = $this->translator->get($key)) !== $key) {
        return $line;
    }

    if (is_bool($value)) {
        return $value ? 'true' : 'false';
    }

    return $value;
}