Application Rules: Apply on certain conditions

I would like to use Association Count Rules but within certain conditions (like an entity on a specific state)
On Conditional application rules mark suggests creating a decorator, but I can’t see a way to pass the RulesChecker or to wrap up the ValidCount in a decorator.

Basically I want to do something like this

public function buildRules(RulesChecker $rules)
    // Conditional like validator
    $rules->add($rules->validCount('photos', 3, '<=', 'You can only have 3 photos'), [
        'on' => function ($entity) {
            return $entity->status >= 3;

    return $rules;

If you were to use the decorator approach I suggested, the rules builder could be a constructor argument to your new rule.

1 Like