Support

Account

Home Forums Front-end Issues Hidden-by-conditional-logic get posted and create problems.

Solved

Hidden-by-conditional-logic get posted and create problems.

  • Hi I noticed a problem with my ACF setup.

    I have repeater rows with about 60 fields in them, I hide the fields and the rows via conditional logic, depending on radio group choices, and when I save the post the fields which are hidden by conditional logic get posted and visible on the front-end, I was reading about this and E. said that the fields are disabled in order to not get posted, but altought they are disabled they still get posted, and this is the result of it :

    http://prntscr.com/bwxta2

    This 4 arrows are 4 completely different fields, they should be just 2, how did that happend:

    I made a post and then I made edit to it, the 3000лв. is Price field, I hide the price field if “По договаряне” checkbox is checked, and so I did in the edit, but when I saved it it had both values, and I can’t delete the Price field.
    Then I edited it again and from a select I choose different model and make in the picture Daf and LeyLand are the choices of my edit, and the old values are still visible, with the new ones.

    What solution am I looking for:
    I was thinking of clearing the values of the fields which has the class acf-clhi in the wrapper class hidden-by-conditional-logic but that didn’t seemed to work for non of the fields using val('') for the input.acf-clhi ,
    so I would be more than happy with a solution of that kind, if the field gets hidden the val gets cleared.

    The second option is to prevent disabled fields from posting, attaching something to the pre-save-posts filter, but I have no idea what this something should be..

    Any help would be highly appreciated, thanks in advance! 🙂

  • If the field never had a value, then in will not have a value saved, but if the field has a value and you change the condition and the field is hidden then whatever value was in that field before will remain in the field.

    An example

    Let say we have something like this
    – Radio Field
    —- choice 1
    —- chioce 2
    – Text field 1, appears for choice 1
    – text field 2, appears for choice 2

    Select choice 1 and enter a value in text field 1 and save the post.

    Edit the post and select choice 2 and enter a value into text field 2 then save the post. The value previously entered into text field 1 will still be in the database.

    This is working as expected in ACF and it requires that you create if conditions when showing the values on the front end. Conditional logic on the back end is strictly for showing and hiding fields on the back end to build a logical user interface and does not control the display of the front end. When a field is hidden by conditional logic that field is not submitted, since it is not submitted, then it is not updated, or removed.

    
    $choice = get_field('radio_field');
    if ($choice == 'choice 1') {
      // show text field 1
      the_field('text_field_1');
    } else {
      // show text field 2
      the_field('text_field_2);
    }
    

    If you want values to be removed when a post is updated then you will need to create an acf/save_post filter that checks the radio field and deletes the value that you no longer want.

  • Greetings John!
    The main problem is that what you say here
    “When a field is hidden by conditional logic that field is not submitted”

    Doesn’t work as expected on my end, it get posted even on the first publish of the post, for example I have Choice 1 that open select field 1 and Choice 2 that open select field 2 and close select field 1, but if I pick an option from select field 1 and choose Choice 2 and enter value in select field 2 too both of the values get posted, and it creates a problem because I have the following fieldset in order to make a dynamic filter.

    Car make : Acura, Aston etc..
    Car Model Acura : Integra, MDX etc..
    Car Model Aston : X70 etc..

    When you choose Acura you have to choose an option from the “Car Model Acura” select field, and if you then change the value to Aston you have to choose from the other car model select, the problem comes on the front-end, because :

    I use

    if (get_field('car-model-acura')) {
    echo the_field('car-model-acura'); 
    }
    if (get_field('car-model-aston')) {
    echo the_field('car-model-aston');
    }

    But on the front end it shows:
    “AcuraAston:IntegraX70”
    Because I have values for both of the selects 🙁
    And each edit is adding a value if there is another car-make or model choosen, the worst part is that the website won’t hold posts published from moderators or administrators it’s front-end posting, and users won’t clear their values, if they decide to change the year of the car for example.

    One thing to mention is that I don’t have a reset value for the select fields, they start like :
    Alpha Romeo
    Acura
    etc..

    instead of

    Choose a model
    Alpha Romeo
    Acura
    etc..

    And maybe if I add these values and use javascript to add them if the field has the acf-clhi class, and then in the backend define that this value shouldn’t display it would be an adequate solution?

    Thanks for your time and descriptive answer! 🙂

  • You need to check the radio field on the front end, not the conditional fields. This would be the case whether or not the value is saved in the admin.

    
    if (get_field('car-model') == 'acura') {
      echo the_field('car-model-acura'); 
    } elseif (get_field('car-model') == 'aston') {
      echo the_field('car-model-aston');
    }
    
  • Yep I tried the elseif approach and I can comfirm it only shows 1 value, but there is still something I need to clear up, when a post get edited it shows the old value in some cases, for example I had a bus with the following info:
    Make : Avia
    Model : Preslav
    The output was : Avia:Preslav

    Now I updated it to:
    Make : Daf
    Model : Leyland
    The output is : Daf:Preslav

    How can I clean up the old fields on post edit, using the save_post or pre_save_post filters? What should I be looking for.

    Also is there a way to remove the values from hidden fields with js if there is class ‘acf-clhi’ instant, like on Choice 2 – Text field 1 get val(“”) because there is class ‘acf-clhi’.

    Thanks for your answer again, it is the one that solved my question I will mark it as a solution, but I would be really thankfull if you could help me with this either. 🙂

  • To clean up old values you need to create and acf/save_post action https://www.advancedcustomfields.com/resources/acfsave_post/

    In this action get the radio field and then get the fields you want to clear and if they have a value use update_field() to clear whatever value is in them.

  • Actually I managed to solve it, by adding extra check before getting and echoing the field with this structure:

    <?php
    $make = get_post_meta($post_id, 'add-info-cat-auto_0_car-stamp');
    if (get_post_meta($post_id, 'add-info-cat-auto_0_car-stamp')) {
        echo $make[0] . ':';
    } if ($make[0] === 'Acura') {
        $model = get_post_meta($post_id, 'add-info-cat-auto_0_car-model-acura');
        echo $model[0];
    } elseif ($make[0] === 'Alfa Romeo') {
        $model = get_post_meta($post_id, 'add-info-cat-auto_0_car-model-alfa-romeo');
        echo $model[0];
    } elseif ($make[0] === 'Audi') {
        $model = get_post_meta($post_id, 'add-info-cat-auto_0_car-model-audi');
        echo $model[0];
    }
    ?>

    Since my car-make options are stored in one select, and models are in different selects it works better than clearing the values because you can’t really have 2 models. Thanks for you help again, have an awesome day! 🙂

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

The topic ‘Hidden-by-conditional-logic get posted and create problems.’ is closed to new replies.