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

speedydan's avatar

Advanced validation

Hi There,

I have a form that has a few variable sections... for example, I have a select dropdown for employment_status. If a user selects 'Employed' then a number of new fields would appear below (employer_name, job_role, annual_salary). But if they selected 'Unemployed' these extra fields would stay hidden.

My question is how I go about validating these with Laravel. I can't just set them as required because sometimes they may just stay hidden. I've read a bit about the custom conditional validation you can do, but didn't quite understand how to implement it, so wondering if someone could help?

Thanks in advance!

0 likes
2 replies
Cronix's avatar

I'd just check to see which option it was and do the specific validation for each one separately. It can get tricky adding a lot of the conditional validation rules. This is just simpler.

// Common rules
$rules = [
    'first_name' => 'required',
    // etc
];

if ($request->employment_status == 'employed') {

    // define additional validation for employed
    $rules['employer_name'] => 'required' //etc

} else if ($request->employment_status == 'unemployed') {

    // define additional validation for unemployed
    $rules['something_else'] => 'required' //etc
} else {
    // Houston, we have a problem
}

$validatedData = $request->validate($rules);
2 likes

Please or to participate in this conversation.