php – laravel validate a date made up of 3 inputs-ThrowExceptions

Exception or error:

On the website I am building I ask the user for their date of birth, in 3 different inputs,

<input name="day" type="text" />
<input name="month" type="text" />
<input name="year" type="text" />

I validate each individual input, but I need to validate that if they have input data into all 3 fields (day and month are optional), that the date is in the past and not the future, this is my Request class as it stands currently,

public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'firstnames' => 'required',
        'lastname' => 'required',
        'dob_day' => 'digits_between:1,2|nullable|between:1,31',
        'dob_month' => 'digits_between:1,2|nullable|between:1,12',
        'dob_year' => 'digits:4|required',
        //need to validate the date of birth if all 3 have values.
    ];
}

/**
 * Get the validation messages that apply to the rules
 *
 * @return array
 */
public function messages()
{
    return [
        'firstnames.required' => 'Please enter any first names',
        'lastname.required' => 'Please enter a last name',
        //'birth_place.required' => 'Please enter a place of birth',
        'dob_month.digits_between' => 'The date of birth\'s month must be no more than 2 characters in length',
        'dob_day.digits_between' => 'The date of birth\'s day must be no more than 2 characters in length',
        'dob_month.max' => 'The date of birth\'s month must be no more than 2 characters in length',
        'dob_year.digits' => 'The date of birth\'s year must be 4 characters in length',
        'dob_year.required' => 'Please enter a year of birth, even if it is an estimate',
        //'dob_accurate.required' => 'Please specify whether the date of birth is accurate'
    ];
}

Is there away I can validate three different inputs as 1 and throw and error if the date is not valid as a date in the past?

How to solve:

You could use javascript to set the date into another hidden field that you validate as in past if the others are filled. Or if you don’t want to use javascript you can do some extra checking in the if statement on the current date inputs and change their rules to fit or throw an exception

public function rules()
{
    $rules = [
        'firstnames' => 'required',
        'lastname' => 'required',
        'dob_day' => ['digits_between:1,2', 'nullable', 'between:1,31'],
        'dob_month' => ['digits_between:1,2', 'nullable', 'between:1,12'],
        'dob_year' => ['digits:4', 'required']
        //need to validate the date of birth if all 3 have values.
    ];
    if (request()->has('dob_day')&&request()->has('dob_month')&&request()->has('dob_year')) {
        $date = now();
        $rules['full_date'] = 'before:'.now()->toDateString();

        //Or if you don't want to use javascript you can do some extra checking here on the current date inputs and change their rules to fit or throw an exception
        $rules['dob_year'][] = 'max:'.now()->year;
        ...
    }
    return $rules
}

Leave a Reply

Your email address will not be published. Required fields are marked *