skip to Main Content

Consider this validation:

Route::post('/validate', function (Request $request): JsonResponse {
    $validated = $request->validate([
        "insurance_plan" => ["required", "in:Commerical Insurance,Government,Uninsured"],
        "insurance_hasSecondaryPrescriptionInsurance" => ["boolean"],
        'insurance_secondaryPrescriptionName' => ['required_if:insurance_hasSecondaryPrescriptionInsurance,true', 'required_if:insurance_plan,Government,Commericial Insurance' ]
    ]);

    return response()->json([
        'validated' => $validated
    ]);
});

For insurance_secondaryPrescriptionName, I’m looking to do a AND validation. If insurance_plan has those value ‘Uninsured’ AND insurance_hasSecondaryPrescriptionInsurance is true.

So if I send out this request it should pass validation:

POST https://localhost/api/validate
Content-Type: application/json
Accept: application/json

{
    "insurance_plan": "Uninsured",
    "insurance_hasSecondaryPrescriptionInsurance": true,
    "insurance_secondaryPrescriptionName": ""
}


Does this require a custom validator or is there a preexisting validation I can use?

2

Answers


  1. Chosen as BEST ANSWER

    I was able to figure this out by extending Validator (based off of this How to use multi required_if in laravel?).

            Validator::extend('required_multiple', function ($attribute, $value, $parameters, $validator) {
                $pool = [];
    
                for ($i = 0; $i < count($parameters); $i = $i + 2) {
                    $parameter = $parameters[$i];
                    $condition = $parameters[$i + 1];
    
                    if (in_array($condition, ['true', 'false'])) {
                        $condition = (bool) $condition;
                    }
    
                    $data = $validator->getData();
    
                    if (strpos($condition, '|') !== false) {
    
                        $haystack = explode('|', $condition);
                        if (in_array($data[$parameter], $haystack)) {
                            $pool[] = true;
                        } else {
                            $pool[] = false;
                        }
                    } else {
                        if ($data[$parameter] === $condition) {
                            $pool[] = true;
                        } else {
                            $pool[] = false;
                        }
                    }
                }
    
                if (in_array(false, $pool)) {
                    return true;
                } else {
                    return false;
                };
            });
        }
    
    

    And I could use it in my code like so:

    'some-field' => ['required_multiple:insurance_plan,Government|Commerical Insurance,insurance_hasSecondaryPrescriptionInsurance,true']
    

  2. To use an "AND" logic or to add more complex validation logics you need to use Rule::requiredIf() to create a custom rule validator using a closure (https://laravel.com/docs/10.x/validation#rule-required-if):

    Route::post('/validate', function (Request $request): JsonResponse {
        $validated = $request->validate([
            // ...
            'insurance_secondaryPrescriptionName' => Rule::requiredIf(function () use ($request) {
                // your own logic
                return true; // or false;
            })
        ]);
    
        return response()->json([
            'validated' => $validated
        ]);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search