alleyinteractive / alleyinteractive/wordpress-fieldmanager

display_if incorrectly hide siblings when matching their `display-src` values

Open
#873 0 comments 0 reactions 0 assignees View on GitHub
bug good first issue javascript scope:display-if ux
Dominant language
PHP
Stars
563
Forks
99
PR merge metrics
No merged PRs in 30d

Description

### Description of the bug

When trying to trigger the show and/or hide fields, `display_if` matches the partial field name of a sibling field instead of performing an exact match.

If you happen to have a `display_if` logic that share a field name, fields with a partial match ends up getting hidden.

The source of the issue seems to be the use of `.match` here, since it is not an exact name match but a partial one (it matches a string based of the field name instead of matching the string with the field name):

https://github.com/alleyinteractive/wordpress-fieldmanager/blob/584979f3e59b2173fa71633a07c39c0050b11347/js/fieldmanager.js#L299

### Steps To Reproduce

Here is an example you can use to replicate this issue:

```php
function add_custom_meta_boxes() {
$custom_meta_group = new Fieldmanager_Group(
array(
'name' => 'custom_meta_group', // <--- display_if will match 'meta' in this field name too, even though it should not.
'serialize_data' => false,
'children' => array(
'meta' => new Fieldmanager_Checkbox( [
'save_empty' => false,
'checked_value' => 'meta',
'label' => 'Checkbox',
]
),
'text_field' => new Fieldmanager_Textfield(
array(
'label' => 'Text Field',
'display_if' => [
'src' => 'meta',
'value' => 'meta',
],
)
),
'textarea' => new Fieldmanager_TextArea(
array(
'label' => 'Text Area',
'display_if' => [
'src' => 'meta',
'value' => 'meta',
],
'attributes' => array(
'rows' => 5,
'cols' => 50,
),
)
),
'subject_prefix' => new Fieldmanager_Select(
[
'name' => 'subject_prefix',
'label' => 'Subject prefix',
'options' => [
'none' => 'None',
'custom' => 'Custom',
],
'default_value' => 'none',
'display_if' => [
'src' => 'meta',
'value' => 'meta',
],
]
),
'subject_prefix_custom' => new Fieldmanager_Textfield(
[
'label' => 'Custom subject prefix',
'display_if' => [
'src' => 'subject_prefix',
'value' => 'custom',
],
]
),
),
)
);

$custom_meta_group->add_meta_box( 'Custom Meta', 'your-post-type', 'normal', 'high' );
}
add_action( 'admin_init', 'add_custom_meta_boxes' );
```

https://github.com/user-attachments/assets/329717ec-d39c-48ba-b747-9d2c0c394531

### Additional Information

I'm working on the assumption that `.match` should match the field name provided in `display-src`, but it is possible there is feature that relies on a loose match.

So this change might require a bit more testing to avoid regressions.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.