Support

Account

Home Forums Front-end Issues Problem to reset conditional fields

Solving

Problem to reset conditional fields

  • Hi,
    I have a problem with conditional logic fields in a group of custom fields.

    I use acf_form() to display custom fields in front end, this allow the user to insert a new custom post called ‘order’. The same user can also modify that order after it is saved in database.

    I use conditional logic to display or hide a specific custom field.

    When a specific custom field is set to a certain value the custom field condinioned to that choice is shown and I can choose a value. This value is write in database.

    If the user modify precedence choice the custom field conditioed is hide, however your previous value not change in database.

    The problem comes when I dispaly in front end all custom field of order using get_fields() function. All meta_value writed in database associated to that post are diplayed, also if value of conditioned field in acf_form() or in back office is hide.

    A possibility to solve this problem is:
    the conditional field, if is hide, his precedent value if exists, because of it is setted in a previous configuration, is deleted in database.
    Also, an other solution can be to tie the function get_fields() at same javascript that the acf_form() uses so: if custom field is ‘hide’ it cannot dispalyed also if exists corresponding value in database.

    How can I do this? Are there other possibility to obtain same result?
    Thanks a lot.
    P.S. Sorry for my basic english.

  • I think you’re going to need to check the value of your field that sets the condition for the field you want to display, and then write the PHP to either show or not show the conditional field. This is what I do when building templates.

    
    if (get_field('condition_field')) {
      the_field('conditional_field');
    }
    

    Not sure how you can translate that with the array returned by get_fields(). The test is going to need to be build into the loop you’re using to show your values, probably defeating the purpose using of get_field() since I’m assuming it is because you have a lot of fields to display.

    I don’t use this function because 99% of my field names begin with “_” (underscore) which makes the function pretty useless for me.

    You’re suggestion of deleting the value if the condition is false I think would be difficult. I think the only way to do this would be to find and delete all value before inserting the new values. My suggestion here would be to use the hook “acf/save_post” and in your hooked function use the WP function “delete_post_meta()” to delete all the meta values you want to have cleared. You may also need to scrub through the $_POST array to remove any values that should not be set. Really, it would be easier to use my first suggestion and do a test when displaying them then to do all of the work involved with clearing values before the save.

    I don’t think this is something that ACF should do by default because everyone may not want this to happen. For example, having the values saved and not cleared makes it easy for users to switch back and forth between 2 options without needing to reenter any data. My clients would find that quite annoying.

    I don’t think the JavaScript used in the admin/forms for showing and hiding conditional fields will work without the form or in templates, you may need to write your own JS to do this.

  • Hi Hube2,
    thanks for reply.
    The solution that you have suggested is not good for me.
    Infact I have a lot of conditionald field that can be changed in time.
    If I write php code to check field’s value everytime that I add new conditional field I would to add php code for that field/fields. This option is not dynamic.
    Also the solution to delete the enterly post is not good because the custom post is a order with a specific id that not must change.
    I would to display the custom fields using the same javascript that use the plugin. If field is hide it should not be displayed using get_fields(). How can I do this? Does that information write somewere? There is any flag or field that contains this information?
    I would not write new Javascript but exploit already existing information.
    Thanks a lot.

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

You must be logged in to reply to this topic.