【问题标题】:Format date in French within a Laravel validation在 Laravel 验证中用法语格式化日期
【发布时间】:2019-07-03 01:33:04
【问题描述】:

在我的控制器中,我想为 2000 年之前的日期创建一个验证。

'dateofbirth' => 'required|date|after:1960|before:2000-01-01'

我的验证有效;但是,我想将2000-01-01 的格式更改为01-01-2000。

在文件validation.php,我想有类似下面这样的消息。

The dateofbirth must be a date before 01-01-2002.

如何转换这个日期?

编辑 02/07/2019

这是我的控制器。这是对的吗 ?

public function store(Request $request)
{
    $request->validate([
       'dateofbirth' => 'required|date|after:1960|before:2000-01-01'   
    ]);

     Student::create($request->all());
     return redirect()->route('students.index')
           ->with('success', 'new data created successfully');
}

public function messages()
{
    return [
     'dateofbirth.required' => "REQUIRED",
     'dateofbirth.date_format' => "MESSAGE",
     'dateofbirth.after' => "MESSAGE",
     'dateofbirth.before' => "The dateofbirth must be a date before 01-01-2002." 
   ];
}

在我的文件validation.php 我应该把这个放在?

'dateofbirth.before' => "The dateofbirth must be a date before 01-01-2002." ,

【问题讨论】:

  • 您是否将该表格发送到您的控制器?你能把你的代码吗?

标签: laravel laravel-validation


【解决方案1】:

你可以的

'timeStart' => 'required|date_format:"Y-m-d"|after:1960|before:2000-01-01'

不确定日期,但第二部分肯定有效!

你可以这样做:

public function rules(){
    return [
      'dateofbirth' => 'required|date|after:1960|before:2000-01-01'
    ]
}

public function messages(){
    return [
      'dateofbirth.required' => "REQUIRED",
      'dateofbirth.date_format' => "MESSAGE",
      'dateofbirth.after' => "MESSAGE",
      'dateofbirth.before' => "The dateofbirth must be a date before 01-01-2002." 
    ]
}

如果我没记错的话,消息必须用双引号引起来。

更多关于验证的信息:https://laravel.com/docs/5.8/validation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2020-11-10
    • 2018-10-21
    • 2018-08-01
    • 1970-01-01
    • 2017-04-04
    • 2010-11-22
    相关资源
    最近更新 更多