Laravel validation error messages: show locale-specific date

raw

AppServiceProvider.php

1
2
3
4
5
6
7
8
9
10
11
12
13
        \Illuminate\Support\Facades\Validator::replacer(
            'after', LocalDateFormatReplacer::class
        );
        \Illuminate\Support\Facades\Validator::replacer(
            'after_or_equal', LocalDateFormatReplacer::class
        );
        \Illuminate\Support\Facades\Validator::replacer(
            'before', LocalDateFormatReplacer::class
        );
        \Illuminate\Support\Facades\Validator::replacer(
            'before_or_equal', LocalDateFormatReplacer::class
        );
 
raw

LocalDateFormatReplacer.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace App\Validators;
 
/**
 * Replace the :date attribute value in a validation error message with the
 * locale-specific date format.
 *
 * Registered in AppServiceProvider.
 *
 * @author Christian Weiske <weiske@mogic.com>
 */
class LocalDateFormatReplacer
{
    /**
     * Replace the :date attribute value with the local date format.
     *
     * @param string $message    Message with ":date" placeholder
     * @param string $attribute  Name of attribute (e.g. "contract_start")
     * @param string $rule       Name of the rule (e.g. "before_or_equals")
     * @param array  $parameters Attributes to dipsplay. Key 0 is the date.
     * @param object $validator  Validator instance
     *
     * @return string Final message with replaced :date
     */
    function replace($message, $attribute, $rule, $parameters, $validator)
    {
        if (strtotime($parameters[0]) === false) {
            //a label, not a time string
            $date = $validator->getDisplayableAttribute($parameters[0]);
        } else {
            $date = reos_date($parameters[0]);
        }
 
        return str_replace(':date', $date, $message);
    }
}
?>
 
 
raw

test.txt


https://laravel.com/docs/5.5/validation


keywords:
- laravel validation date format locale
- laravel validation error message adjust data
- customize error message data


vendor/laravel/framework/src/Illuminate/Validation/Validator.php
vendor/laravel/framework/src/Illuminate/Validation/Concerns/FormatsMessages.php
makeReplacements

ReplacesAttributes.php
  replaceBefore
getDisplayableAttribute

ac4178d92561bd46dd8640cd5825b41d2a4759c3


addReplacer
Christian Weiske Christian Weiske
owner

History