How to create complex dependent select attributes

Hi, I would like to create dependent select attributes (2 or more attributes) like make, then model, then body type, etc.

I have read all of the other posts referring to the same dependent attribute feature, and am aware of the workaround using a parent-child attribute as shown in this answer.

However, I’m wondering if anyone can give me some direction on how to do a custom implementation for this feature. Again, I am aware of the work around but I need more complex dependencies. Any rough or detailed guidance as to a viable custom approach would be very helpful.

Thank you.

Hi,

Unfortunately there’s no simple code snippet, but I can provide general guidance if you’re familiar with coding.

The easiest way would require adding custom JS and PHP code. The JS code, to which fields depend on which ones, and add/remove the HTML “required” attribute. For example, you can pass a custom parameter like “_parent” to the field array of parameters (via the form hooks, e.g. for the listing form hivepress/v1/forms/listing_update), with the parent field name. Then, using hivepress/v1/fields/field hook, you can check if this parameter is passed (by checking $field->get_args()), and add a HTML attribute like data-parent=“parent_field_name_here”. With JS, you can then listen to the parent field changes and if it changes, make the child field required or not (and optionally hide child fields or show them, this way the form content will depend on the selected parent field values).

Additionally, you can also check this logic on the back-end via the hivepress/v1/forms/listing_update/errors filter hook, otherwise with front-end-only logic (via JS), parent/child field requirements can be skipped if someone knows how to disable JS on a page.

Hope this helps

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.