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

beracah.kings's avatar

date validation

im collecting date in the form of seperate select option for day, month and year. Im concatanating before updating in database. How to validate the date and send error message if any one of the select option is null

0 likes
8 replies
Nakov's avatar

@beracah.kings why not first validating if each of the field is present, and then just validate if it is a valid date or not?

'day' => 'required',
'month' => 'required',
'year' => 'required'

You can add additional rules here, for validating that the day is not bigger then 31, month no more then 12 or if it is a month name that it exists in array of names, same for year. And once you concatenate you can validate if it is a valid date at all.

beracah.kings's avatar

@nakov thanks for the reply. @himanshurajvanshi thank you.

Im getting the dates in the register controller. how to send error message in the register controller. how to pass custom error msgs in the validator function

beracah.kings's avatar

 protected function validator(array $data)
    {
            return Validator::make($data, [
                'email' => ['required', 'email', 'max:255','unique:partners'],
                'mobile' => ['required', 'min:10', 'max:10','unique:partners'],        

            ], [
                'email.required' => 'Email is required.',
                'email.email' => 'Email needs to have a valid format.',
                'email.unique' => 'Email is already registered.',
                'mobile.required' => 'Mobile Number is required.',                
                'mobile.min' => 'Mobile Number must be at least 10 characters',
                'mobile.max' => 'Mobile Number must be at least 10 characters.',
                'mobile.unique' => 'Mobile Number is already registered.',
         
           ]);
    }

This one worked for me. However numeric and min /max doesnt work when used together.

Nakov's avatar

@beracah.kings but you have 10 for both, so what do you mean it does not work? Why don't' you use just digits:10 for example?

petrit's avatar

You may use date_format rule

'day' => 'required|date_format:d',
'month' => 'required|date_format:m',
'year' => 'required|date_format:Y'

Please or to participate in this conversation.