Home › Forums › General Issues › Null values for hidden conditional fields
This is going to be tricky to explain, but i’ll try…
I’ve set up my cfs with conditions. For example – only display B field in the admin if C field has X value present. If C field has value Z then display A field, make sense so far?
So… In C field choose value Z and A field appears then I choose a value 123 from A field. I update my post. I then change my mind and change C field value to X so that A field disappers and B field appears in which I choose a value of 789. I then update my post again.
Now in my template I have this piece of code for every field… essentially it only outputs the field’s value IF a value has been chosen in the first place – if no value has been chosen, nothing is echoed/outputted.
Because I originally chose the value 123 for field A this field is outputted, even though I changed my mind and chose the value 789 for field B instead. I would have hoped because the conditional I activated made field A disappear then field A would essentially be seen has having no value in my template tags.
The only way around is to go back into field A and give it a null value before going back to change the field to B…
So, my question is… If a field is hidden due to a conditional can that hidden field somehow be considered to have a null value until such time it is visible again on the post edit/admin page?
If not possible, and I know this is for the feature request forum, could there be a checkbox with the conditional settings of a field that says “give this field a null value if hidden”.
Thanks 🙂
Hi @pjeaje
Yes, you have explained it clearly. Good job on that.
The answer is not so simple however. When a field is hidden via conditional logic, it also becomes disabled. This prevents ACF from posting and saving the hidden value.
This stops the event of having 20 fields (hidden by conditional logic) entering null values into the DB.
I’m a bit nervous to add any code in ACF which actually removes a value – I can just see the support tickets flooding in about issues regarding this.
The simple answer is to make your template a bit more robust. Am I right in assuming that you are usig a function such as get_field_objects
to get all values and simple loop through them and output the value if there is one?
Is it possible for you to move away from this loop style coding? Towards a more strict code which you can then check the value of field C and take appropriate action?
Thanks
E
The topic ‘Null values for hidden conditional fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.