Laravel 5.6出生日期验证

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.

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:

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:

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?

dongtang4954
dongtang4954 尝试'y-m-d'而不是'Y-M-D'
2 年多之前 回复
dongyang7152
dongyang7152 尝试'date_of_birth'=>'date_format:Y-M-D|before:today|nullable',
2 年多之前 回复

3个回答

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

Here you will find the date rules for different conditions:

$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-d format. Not this Y-M-D. Month and Date parameter must be in lower case. Hope it will solve your issues.



只是一个额外的想法...... </ p>

我将nullable添加到date_of_birth然后得到一个SQL 错误声明date_of_birth不允许为null。 我忘了将列设置为允许我的架构中的空值</ p>

谢谢,这些建议有帮助</ p>
</ div>

展开原文

原文

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

Thanks, the suggestions helped

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐