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

croftCoder's avatar

Laravel Validate and Remember Checkbox Input and Date

i have a form where the user can select multiple certificates they have but for each selected certificate they have to input the date it was received. trying to figure out:

  1. make the date input for that checkbox required if the checkbox is checked
  2. how to get and validate the specified date for that input like: date|before:today
  3. remember old data in form for date input if validation fails

i'm using FormRequests to validate inputs.

the ???? means how to catch errors for the date input of that checkbox if any

<input type="checkbox" name="certificates[]" value="bachelor" id="bachelor">
<label for="bachelor">Bachelor's Degree</label>
<input type="date" name="date_received" value="{{ old('date_received') }}" class="@error('????') border-red-500 @enderror">

<input type="checkbox" name="certificates[]" value="master" id="master">
<label for="master">Master's Degree</label>
<input type="date" name="date_received" value="{{ old('date_received') }}" class="@error('????') border-red-500 @enderror">

FormRequest below

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SaveMembershipRequest extends FormRequest {

    public function authorize() {
        return true;
    }

    public function rules() {
        
        return [
            'certificates.*' => [
            	// HOW TO FILL THIS PART
            ],
        ];
    }

    public function messages() {

        return [
            // WHAT GOES HERE
            'certificates.*' => 'we do not know about this certificate',
            'certificates.*.date_received' => 'we do not know about this certificate',
        ];
    }
}
0 likes
1 reply
saifqureshi341's avatar

You can use HTML just like this

<input type="checkbox" name="certificates[0][certifcate]" value="bachelor" id="bachelor">
<label for="bachelor">Bachelor's Degree</label>
<input type="date" name="certificates[0][date_received]" value="{{ old('certifcates["0"]["date_received"] }}" class="@error('????') border-red-500 @enderror">

<input type="checkbox" name="certificates[1][certifcate]" value="master" id="master">
<label for="master">Master's Degree</label>
<input type="date" name="certificates[1][date_received]" value="{{ old('certifcates["1"]["date_received"]') }}" class="@error('????') border-red-500 @enderror">

Then in the Form request, you can do something like this :

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SaveMembershipRequest extends FormRequest {

    public function authorize() {
        return true;
    }

    public function rules() {
        
        return [
				'certificates' => "array|min:1", // Should contain atleast one certifcate.
            	'certificates.*.certificate' => "required",
				'certificates.*.date_received' => "required|date|before:today",
        ];
    }

    public function messages() {

        return [
            // WHAT GOES HERE
            'certificates.*' => 'we do not know about this certificate',
            'certificates.*.date_received' => 'we do not know about this certificate',
        ];
    }
}

Please or to participate in this conversation.