访问其他帖子字段以创建复杂的验证条件

I'm sending request data with parent/child relationships (1 level max depth). So parent_id references itself in the table as a fk.

debug($this->request->getData()) yields:

[
    "activities": [
        [
            "name": "Parent Activity",
            "id": 3,
            "parent_id": null,
            "quantity": null,
        ],

        [
            "name": "Child Activity",
            "id": 4,
            "parent_id": 3,
            "quantity" :5
        ],

        [
            "name": "Parent Activity 2",
            "id": 7,
            "parent_id": null,
            "quantity": null,
        ],

    ]
]

I want to create a Validation rule that will force parent activities who have no children ( nodes with parent_id pointing to them ) to have quantity" filled in (this can be any arbitrary field, I have a few others).

The most straight forward way I can think is by looping/manipulating the request data and marking it, but is there a nice Cake way of doing this and possibly in the Validation?

$data = $this->request->getData();
$collection = new \Cake\Collection\Collection($data);

// filter/map so I can create a new key to count the # of children, thus manipulating the data as such:

[
    "activities": [
        [
            "name": "Parent Activity",
            "id": 3,
            "parent_id": null,
            "quantity": null,
            "children": 1
        ],

        [
            "name": "Child Activity",
            "id": 4,
            "parent_id": 3,
            "quantity" :5
        ],

        [
            "name": "Parent Activity 2",
            "id": 7,
            "parent_id": null,
            "quantity": null,
            "children": 0
        ],

    ]
]

Then I could use the children as a condition in Validation rather than something more complicated. Though manipulating the request data doesn't seem that clean. Open to any other suggestions.

Using Cake3.

Use conditional validation.

Example taken from the link above:

$validator->add('picture', 'file', [
    'rule' => ['mimeType', ['image/jpeg', 'image/png']],
    'on' => function ($context) {
        return !empty($context['data']['show_profile_picture']);
    }
]);

$context is the data, just add your logic to check $context in the closure.