4

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?

jproux
  • 265
  • 1
  • 6
  • 16

4 Answers4

7

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.

Md Riadul Islam
  • 1,273
  • 11
  • 25
4
'date_of_birth' => 'nullable|date_format:Y-m-d|before:today',
Erkan Özkök
  • 895
  • 12
  • 25
1

i think an easier approach would be

'date_of_birth' => 'required|date|date_format:Y-m-d|before:'.now()->subYears(18)->toDateString()

assuming valid age is 18 years

Kevin Owino
  • 472
  • 3
  • 7
0

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

jproux
  • 265
  • 1
  • 6
  • 16