Home Forums General Issues Saving a hidden select field without a value


Saving a hidden select field without a value

  • Suppose, I have two select fields: “Metro station” and “City district”. The fields are mutually exclusive. So, when there is something selected in the “Metro station” field, the “City district” field becomes invisible. If the field is visible, it require a value to be selected.

    So, I’ve used the conditional logic functionality to control the fields visibility. Then, I made both fields required and allowed Null values, so that the user is able to deselect the value from one field making another field appear. Now imagine the situation. A user creates a page selecting some value from the “Metro station field”, the “City district” field becomes invisible in this case and so not required. Then user decides to set a value for the “City district” field instead of the “Metro station” field. He deselects the value in “Metro station”, the “City district” field becomes visible. Then he selects a value for the “City district” (“Metro station” disappears) and saves the page. If you repeat these actions, after reloading the page, you see the value of the “Metro station” left untouched. As I understand, since the field is required, when we deselected the value and saved the page, the old value haven’t been rewritten with Null. If we deselect the value of the “Metro station” field again, we’re gonna see the value of the “City district”, we’ve selected on the previous step.

    So, are there any methods to satisfy all the requirements:

    1. Have two required fields with ability to mutually switch their visibility (when one field is visible, another one is invisible)?
    2. Have an ability to deselect the value of the field making another field appear?
    3. Have an ability to save newly selected value of the other field instead of the currently selected one?

  • The easiest way to accomplish this is to have a radio field that allows selecting what type of value to enter with both of the select fields being conditional on the radio field. Then in you template you get the value of the radio field and display the correct content.

    If you want to remove the content of the other field that is hidden you need to create an acf/update_value filter This will only run when a field is being updated, when one field is update you delete the value from the other field

  • Another solution is to use ACF Javascript API and manage onchange event on your fields then add/remove the disable class when required.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.