Laravel 5.6 出生日期验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50569919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:45:50  来源:igfitidea点击:

Laravel 5.6 Date of Birth Validation

phplaravel

提问by jproux

I created validation rules in my Member Model with date of birth NOT being required. However, when someone does fill in their date of birth, I want it to be 1. in the correct format and 2. the date cannot be a date in the future.

我在我的成员模型中创建了验证规则,不需要出生日期。但是,当有人填写他们的出生日期时,我希望它是 1. 格式正确,2. 日期不能是将来的日期。

However, when I fill out my form and leave date of birth empty, I still get the following error:

但是,当我填写表格并将出生日期留空时,我仍然收到以下错误:

enter image description here

在此处输入图片说明

Here my basic Member.php model:

这是我的基本 Member.php 模型:

use Esensi\Model\Model;

class Member extends Model
{
    protected $rules = [
        'name' => 'required|alpha|min:2|max:255',
        'surname' => 'required|alpha|min:2|max:255',
        'id_number' => 'required|unique:members,id_number|digits:13',
        'mobile_number' => 'required|digits:10',
        'email' => 'required|email',
        'date_of_birth' => 'date_format:Y-M-D|before:today',
    ];
}

Here's my MemberController.php store function:

这是我的 MemberController.php 存储函数:

public function store(Request $request) 
{
    $member = new Member;
    $member->name = $request->name;
    $member->surname = $request->surname;
    $member->id_number = $request->id_number;
    $member->mobile_number = $request->mobile_number;
    $member->email = $request->email;
    $member->date_of_birth = $request->date_of_birth;

    if(!$member->save()){
        $errors = $member->getErrors();

         return redirect()
            ->action('MemberController@create')
            ->with('errors', $errors)
            ->withInput();
}
//successful creation
    return redirect()
        ->action('MemberController@index')
        ->with('message', '<div class="alert alert-success">Member Created Successfully!</div>');

}

How do I fix it so that date of birth only shows an error when the field is actually filled in but the formatting is incorrect?

如何修复它以便出生日期仅在实际填写该字段但格式不正确时才显示错误?

回答by Md Riadul Islam

Here you will find the daterules for different conditions:

在这里,您将找到date不同条件的规则:

$v = Validator::make(
  ['start_date' => date('30-05-2028')], // input
  [
    'date_of_birth' => ['before:5 years ago'],
    'start_date'    => 'required|date_format:d-m-Y|after:8 years',
    'end_date'      => 'date_format:d-m-Y|after:start_date', 
    ]
);

if ($v->passes())
 dd('Your Date format is correct.');
else
 dd($v->errors());

For your case you should try:

对于您的情况,您应该尝试:

'date_of_birth' => 'date_format:Y-m-d|before:today|nullable'

Careful: You must use this Y-m-dformat. Not this Y-M-D. Month and Date parameter must be in lower case. Hope it will solve your issues.

小心:您必须使用这种Y-m-d格式。不是这个Y-M-D。月份和日期参数必须小写。希望它能解决您的问题。

回答by Erkan ?zk?k

'date_of_birth' => 'nullable|date_format:Y-m-d|before:today',

回答by jproux

Just an extra thought...

只是一个额外的想法......

I added nullable to date_of_birth and then got a SQL error stating that date_of_birth is not allowed to be null. I forgot to set the column to allow nulls in my schema

我将可空添加到 date_of_birth ,然后收到一个 SQL 错误,指出 date_of_birth 不允许为空。我忘记将列设置为允许我的架构中的空值

Thanks, the suggestions helped

谢谢,建议有帮助