CakePhp 3: Create Validation with dynamic fields

207 Views Asked by At

I'd like to create a complex validate:

I have 4 tables:

products
    id
    name
    price
    attribute_set_id

attribute_sets
    id
    name

attributes
    id
    attribute
    is_required

rl_attributes_attribute_sets
    id
    attribute_id
    attribute_set_id

Now imagine this scenario:

My product belongs to an attribute set. In this set, has some attributes, like "ram, storage, video". ram and storage are required, video isn't.

I have a product entity like that:

product[
    'name' => 'name',  //some name
    'price' => '19.99',     //some price
    'attribute_set_id'  => 4,   //the attribute set id
    'ram'  => '4gb',
    'storage' => '1tb'
]

I'd like to validate, I just can add rules to fields that I know, however, I want to add some rules depending on the fields from the attribute set (that I can create/update/delete), in this case it has these three fields, two of then are required.

How can I add those dynamic rules?

0

There are 0 best solutions below