Indicative: required_if

Am I using this correctly?

            "options": "required|array",
            "options.*.id": "string",
            "options.*.name": "required|string",
            "options.*.active": "boolean",
            "options.*.options": "array",
            "options.*.options.*.id": "required_if:options.*.options|string",
            "options.*.options.*.name": "required_if:options.*.options|string",
            "options.*.options.*.active": "required_if:options.*.options|boolean",

I want that the options..options fields to be required only of the options..options exists, but it’s not working… If I pass and object without the active property, for example, it will pass the validator without errors

That’s because options.*.active is missing required
Should work fine if you add it

1 Like

but it already has the required alternative: “required_if”… Do I have to put the required anyway? if I put the options..options..active: “required” it will be always required and I don’t want that, I want it required only when the options.*.options exists

Can you give 2 sets of example input data, one that should fail and one that should pass?

Should pass:

{
    "options": [
        "id": "s5tvertf5",
        "name": "option name",
        "active": true
    ]
}

Should pass:

{
    "options": [
        "id": "s5tvertf5",
        "name": "option name",
        "active": true,
        "options": [
            {
                 "id": "desyv6ry",
                 "name": "suboption name",
                 "active": true
            }
        ]
    ]
}

Should not pass

{
    "options": [
        "id": "s5tvertf5",
        "name": "option name",
        "active": true,
        "options": [
            {
                "id": "dsds",
                "name": "dsadsad",
                // no active properties
            }
        ]
}