之前:18年前Laravel验证器消息无法正常工作

时间:2018-04-10 17:08:54

标签: php laravel pyrocms

我对Laravel很新。所以请放轻松我。

我收到的消息是“之前:18年前”的日期验证器。

The "" must be a date before 18 years ago.

我该如何定制?我需要编译一些东西吗?

我在validation.php中有这个,它位于resources / lang / en /

'custom' => [
    'dob' => [
        'before' => 'Invalid Age'
    ],
 ],

我正在使用FormBuilder

https://pyrocms.com/help/developer-tools/form-builders/creating-a-form-builder

'dob' => [
        'type'   => 'anomaly.field_type.datetime',
        'config' => [
            "mode"          => "date",
            "date_format"   => "j F, Y",
             "year_range"    => "-100:-18",
             ],
        'rules'  => [
            'required','before:18 years ago' 
         ],

       ],

2 个答案:

答案 0 :(得分:2)

您可以通过在消息文本中指定属性来自定义验证消息:

$messages = [
    'dob' => 'The :attribute must be a date before 18 years ago.',
];

当传递给验证器实例时,:attribute占位符将被替换。

$validator = Validator::make($input, $rules, $messages);

使用语言文件,您可以定义相同的消息文本:

'custom' => [
   'dob' => [
       'before' => 'The :attribute must be a date before 18 years ago.'
   ],
],

然后可选择为该属性指定一个自定义名称:

'attributes' => [
    'dob' => 'date of birth',
],
  

您可以使用__ helper函数从语言文件中检索行。 __方法接受翻译字符串的文件和键作为其第一个参数。

Custom Validation Error Messages

Localization

答案 1 :(得分:1)

我解决了以下

  

在FormBuilder中

dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  

在app / Providers / AppServiceProvider.php

'dob' => [
    'type'   => 'anomaly.field_type.datetime',
    'rules'  => ['required', 'older_than'],
    'config' => [
        'mode'        => 'date',
        'date_format' => 'j F, Y',
        'year_range'  => '-100:-18',
    ],
],