Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nuna's avatar
Level 1

how to validate the birthdate.The birth date is inputted separately.Please help.

protected function validator(array $data)
    {
       
        $today_year= Carbon::now()->format('Y');
        $today_month= Carbon::now()->format('m');
        $today_date= Carbon::now()->format('d');
        
        return Validator::make($data, [
          
            'day' => ['required','date_format:d','before:today_date'],
            'month' => ['required','date_format:m','before:today_month'],
            'year' => ['required','date_format:Y','before:today_year'],
            
        ]);
    }
0 likes
21 replies
automica's avatar

@nuna I would make single 'dob' for your date components and then pass that to your Validator

      $today_year= Carbon::now()->format('Y');

    $data['dob'] = $data['day'].'-'.$data['month'].'-'.$data['year'];

    return Validator::make($data, [
	'day' => ['required'],
	'month' => ['required'],
	'year' => ['required','before:today_year'],
        'dob'=> 'required|date',

    ]);

you don't want to force before check on day and month as if my birthday is 31st December it would always fail.

nuna's avatar
Level 1

I tried this solution but I am getting Bad Method Call.Please help

Sinnbeck's avatar

Which of the solutions? And what is the exact error message?

Sinnbeck's avatar

This might work for you. Borrowed partly from @automica

$data['dob'] = $data['day'].'-'.$data['month'].'-'.$data['year'];

    return Validator::make($data, [
	'day' => ['required'],
	'month' => ['required'],
	'year' => ['required'],
        'dob'=> 'required|'before:' . now()->toDateString(),

    ]);
nuna's avatar
Level 1

Method Illuminate\Validation\Validator::validateRequired|before does not exist.

It comes out error like this.


protected function validator(array $data)
    {
        $passwordRegex = $this->getPasswordRegex();

        $today_year= Carbon::now()->format('Y');
        $data['dob'] = $data['day'].'-'.$data['month'].'-'.$data['year'];

        return Validator::make($data, [
        
            'day' => ['required'],
            'month' => ['required'],
            'year' => ['required','before:today_year'],
            'dob'=> ['required|before:' . now()->toDateString()],
           
        ]);
    }

Sinnbeck's avatar

Oh sorry bad syntax. Fixed

protected function validator(array $data)
    {
        $passwordRegex = $this->getPasswordRegex();

        $data['dob'] = $data['day'].'-'.$data['month'].'-'.$data['year'];

        return Validator::make($data, [
        
            'day' => ['required'],
            'month' => ['required'],
            'year' => ['required'],
            'dob'=> ['required','before:' . now()->toDateString()],
           
        ]);
    }
nuna's avatar
Level 1

No error coming...but it is not validating.It allows even I enter 9/12/2020.The user is not even born

Sinnbeck's avatar

Try year first

protected function validator(array $data)
    {
        $passwordRegex = $this->getPasswordRegex();

        $data['dob'] = $data['year'].'-'.$data['month'].'-'.$data['day'];

        return Validator::make($data, [
        
            'day' => ['required'],
            'month' => ['required'],
            'year' => ['required'],
            'dob'=> ['required','before:' . now()->toDateString()],
           
        ]);
    }
nuna's avatar
Level 1

Its working fine with year.I think I need to get the input of user from blade itself as date of birth.How to do that.My blade file is like this

<div class="row selectpicker-row mb-2">
                                <p class="form-lable2 color-g font14  mt-3 mb-0 ml-3 dob-lable-mobile">
                                    {{ __('Date of Birth') }} <span class="text-danger pr-2">*</span></p>
                                <div class="input-group col-md-4 mt-3 mb-2">
                                    <div class="input-group-prepend mr-0 dob-lable-web">
                                        <span class="form-lable2 color-g font14">{{ __('Date of Birth') }}</span>
                                        <span class="text-danger pr-2">*</span>
                                    </div>
                                    @php
                                    $day='';
                                    $month='';
                                    $year='';
                                    if(isset(auth()->user()->dob))
                                    {
                                    $dob=explode('-', auth()->user()->dob);
                                    $day=$dob[2];
                                    $month=$dob[1];
                                    $year=$dob[0];
                                    }
                                    @endphp
                                    <select name="day"
                                        class="form-control  height40 form-select text-center selectpicker @error('day') is-invalid @enderror">
                                        <option value="">{{ __('Day') }}</option>
                                        @for($i=1; $i<=31; $i++) <option value="{{ $i }}" @if($day==$i)
                                            {{ __('selected') }} @elseif(old('day')==$i) {{ __('selected') }} @endif>
                                            {{ $i }}</option>
                                            $i++
                                            @endfor
                                    </select>
                                    @error('day')
                                    <span class="invalid-feedback dob-ml-20" role="alert">
                                        <strong>{{ $message}}</strong>
                                    </span>
                                    @enderror
                                </div>

                                <div class="input-group col-md-5 mt-3 mb-2">
                                    <select name="month"
                                        class="form-control height40 form-select text-center selectpicker @error('month') is-invalid @enderror">
                                        <option value="">{{ __('Month') }}</option>
                                        <option value="01" @if($month=="01" ) {{ __('selected') }}
                                            @elseif(old('month')=="01" ) {{ __('selected') }} @endif>{{ __('Jan') }}
                                        </option>
                                        <option value="02" @if($month=="02" ) {{ __('selected') }}
                                            @elseif(old('month')=="02" ) {{ __('selected') }} @endif>{{ __('Feb') }}
                                        </option>
                                        <option value="03" @if($month=="03" ) {{ __('selected') }}
                                            @elseif(old('month')=="03" ) {{ __('selected') }} @endif>{{ __('March') }}
                                        </option>
                                        <option value="04" @if($month=="04" ) {{ __('selected') }}
                                            @elseif(old('month')=="04" ) {{ __('selected') }} @endif>{{ __('April') }}
                                        </option>
                                        <option value="05" @if($month=="05" ) {{ __('selected') }}
                                            @elseif(old('month')=="05" ) {{ __('selected') }} @endif>{{ __('May') }}
                                        </option>
                                        <option value="06" @if($month=="06" ) {{ __('selected') }}
                                            @elseif(old('month')=="06" ) {{ __('selected') }} @endif>{{ __('June') }}
                                        </option>
                                        <option value="07" @if($month=="07" ) {{ __('selected') }}
                                            @elseif(old('month')=="07" ) {{ __('selected') }} @endif>{{ __('July') }}
                                        </option>
                                        <option value="08" @if($month=="08" ) {{ __('selected') }}
                                            @elseif(old('month')=="08" ) {{ __('selected') }} @endif>{{ __('Aug') }}
                                        </option>
                                        <option value="09" @if($month=="09" ) {{ __('selected') }}
                                            @elseif(old('month')=="09" ) {{ __('selected') }} @endif>{{ __('Sep') }}
                                        </option>
                                        <option value="10" @if($month=="10" ) {{ __('selected') }}
                                            @elseif(old('month')=="10" ) {{ __('selected') }} @endif>{{ __('Oct') }}
                                        </option>
                                        <option value="11" @if($month=="11" ) {{ __('selected') }}
                                            @elseif(old('month')=="11" ) {{ __('selected') }} @endif>{{ __('Nov') }}
                                        </option>
                                        <option value="12" @if($month=="12" ) {{ __('selected') }}
                                            @elseif(old('month')=="12" ) {{ __('selected') }} @endif>{{ __('Dec') }}
                                        </option>
                                    </select>
                                    @error('month')
                                    <span class="invalid-feedback cstm-ml-14" role="alert">
                                        <strong>{{ $message}}</strong>
                                    </span>
                                    @enderror
                                </div>

                                <div class="input-group col-md-3 mt-3 mb-2">
                                    <select name="year"
                                        class="form-control height40 form-select text-center selectpicker @error('year') is-invalid @enderror">
                                        <option value="">{{ __('Year') }}</option>
                                        @for($i=1972; $i<=2020; $i++) <option value="{{ $i }}" @if($year==$i)
                                            {{ __('selected') }} @elseif(old('year')==$i) {{ __('selected') }} @endif>
                                            {{ $i }}
                                            </option>
                                            $i++
                                            @endfor
                                    </select>
                                    @error('year')
                                    <span class="invalid-feedback cstm-ml-14" role="alert">
                                        <strong>{{ $message}}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>
Sinnbeck's avatar

I am unsure of what you mean? Get the user in blade? What for? I thought the question was regarding validation? Is the validation solved?

nuna's avatar
Level 1

Because I think its not comparing individually right or can compare individually in the controller?

Sinnbeck's avatar

Why do you want to compare them? Shouldn't you just check that the date is valid and before today? Then you save them individually :)

nuna's avatar
Level 1

You are right I want to validate the date before the user input it to make sure the birth-date is relevant

nuna's avatar
Level 1

Back to this if I validate for dob its not working

protected function validator(array $data)
    {
        $passwordRegex = $this->getPasswordRegex();
        $data['dob'] = $data['day'].'-'.$data['month'].'-'.$data['year'];
        
        
        return Validator::make($data, [
        
            'day' => ['required'],
            'month' => ['required'],
            'year' => ['required'],
            'dob'=> ['required','before:' . now()->toDateString()],
            
        ]);
    }
Sinnbeck's avatar

What isn't working? Are you getting an error?

If you wish to validate before submit, you need to use Javascript instead of php

automica's avatar

@nuna you shouldn't need to split your dob back into its component parts. You should have access to the original 3 date parameters as part of your initial request.

the duplication on this:

    <option value="01" @if($month=="01" ) {{ __('selected') }}
                                            @elseif(old('month')=="01" ) {{ __('selected') }} @endif>{{ __('Jan') }}
                                        </option>

is a bit clumsy

I'd just define an array called $months in your controller, pass it in and @foreach through that

also

    @for($i=1972; $i<=2020; $i++) <option value="{{ $i }}" @if($year==$i)

you should define a $yearMin & $yearNow in your controller and pass it in.

Blade files should be simple to read. DRY'ing up views helps massively in cognitive load when trying to debug.

BTW it looks like my answer helped with your validation issue. if the scope of your question has changed, feel free to close this one and open another.

1 like
martinbean's avatar
Level 80

@nuna You could make your life easier and just use a single input to input the birth date. Most browsers have supported the date input type for a while now.

nuna's avatar
Level 1

I changed to datepicker and called the date in the controlelr.It workded.Thank you everyone.

tetranyble's avatar

Maybe in a few days from now someone would run into the same issue. So here's what i think is the best validation:

'birthday'=> 'required|date_format:Y-m-d|after:now', 

And i also think the date should be combined.

diractorhm's avatar

Hmm... Ii would say after is wrong. So here is what I do, and it works perfectly for me:

'birthday' => 'required|before_or_equal:today',

In my lang/de/validation.php I then put a custom message that appears when validating fields that are named birthday:

'custom' => [
  'birthday' => [
    'before_or_equal' => 'Das Geburtdatum kann nicht in der Zukunft liegen!'
  ]
]

Hope this helps.

1 like

Please or to participate in this conversation.