Support

Account

Home Forums ACF PRO Problems with group fields (when saving profile data)

Solving

Problems with group fields (when saving profile data)

  • Hi there,

    I’m creating a form for users to fill in profile data from the front end.

    I pull sub fields from several different groups into these forms.

    And a very strange problem is occurring. When I retrieve the pre-existing values of the profile using the field name, I can retrievew them just fine. When I use the field key however, no data is retrieved.

    However when SAVING the form, no data is saved when I use the field name, but it is when I use the field key. However the data saved using the field key does not show up in the user profile. It’s clearly connected to the user somehow, it’s just not displaying in the backend profile page.

    This is really stumping me and I have no idea what the problem is.

    Is this intentional behavior? Can anyone help?

    Thank you!

    Eivind

  • I would need a lot more information to figure this out, however, a starting place is that group fields behave almost exactly like repeater fields and must be saved in a similar way. So if you’re only getting a single field that is in the group field for your form and not the entire group then this will be a problem. If you’re doing this then ACF is probably seeing the field as just a top level field and not as a sub field of the correct group.

  • Thank you so much for responding, John.

    I believe we’re talking about slightly different things.

    Let me share some code with you.

    $fields_goals = array(
        'ryit_user_legacy_vision', //vision
        'ryit_user_legacy_mission', //mission
        'ryit_user_goals_ten_year', //ten year goal
        'ryit_user_goals_five_year', //five year goal
        'ryit_user_goals_one_year' //one-year goal
      );
    
      $fields_interests = array(
        'ryit_user_interests_expertise', //fields of interest or skill
        'ryit_user_interests_study' //more info about your interests
      );
    
      $fields = array(array("Vision & Roadmap", $fields_goals), array("Purpose & Business", $fields_interests));
    
      $active_field = 'vision-roadmap';
      $echo .= '<div class="main" active-field="' . $active_field . '">';
      
      //Set up category tabs
      $echo .= '<ul class="tabs">';
      foreach($fields as $field) {
        $echo .= '<li id="tab-' . sanitize_title($field[0]) . '"';
        if($active_field == sanitize_title($field[0])) {
          $echo .= 'class="active"';
        }
        $echo .= '>' . $field[0] . '</li>';
      }
      $echo .= '<li id="tab-all">View all</li>';
      $echo .= '</ul>';
    
      $fields_with_val = 0;
      $fields_echo = "";
    
      //Set up fields inside categories
    
      foreach($fields as $field_group) {
        $echo .= '<div class="field-group" id="field-group-' . sanitize_title($field_group[0]) . '">';
        $fields = $field_group[1];
        $fields_echo = "";
    
        foreach ($fields as $field) {
    
          //get essential profile data
          $field_obj = get_field_object($field, 'user_' . $user_id);
          
          if(!empty($field_obj['value']) || ($user_id == $current_user_id)) {
            $fields_with_val++;
            $can_edit = ($user_id == $current_user_id) ? " can-edit" : "";
            $fields_echo .= '<div id="' . $field_obj['name'] . '" class="field' . $can_edit . '">';
            $fields_echo .= '<h3>' . $field_obj['label'] . '</h3>';
            $fields_echo .= '<div class="field-data">';
            if(empty($field_obj['value']) && ($user_id == $current_user_id)) {
              $field_echo = '<div class="field-content"><p class="instructions">Instructions: ' . $field_obj['instructions'] . '</p><p class="add-response button">Add your response</p></div>';
            }
            else {
              if(is_array($field_obj['value'])) {
                $i = 0;
                $val_output = ""; 
                foreach($field_obj['value'] as $val) {
                  if($i>0) {
                    $val_output .= ", ";
                  }
                  $val_output .= $val;
                  $i++;
                }
                $field_echo = '<div class="field-content">' . $val_output . '</div>';
              }
              else {
                $field_echo = '<div class="field-content">' . $field_obj['value'] . '</div>';
              }
            }
            $fields_echo .= $field_echo;
            //echo "user id: " . $user_id;
            //echo "current user id: " . $current_user_id;
    
            if($user_id == $current_user_id) {
              $settings = array(
                'post_id' => 'user_' . $user_id,
                'html_updated_message' => '',
                'fields' => array($field),
                'id' => 'form_' . $field_obj['name']
              );
              ob_start();
              acf_form($settings);
              $form = ob_get_clean();
              $fields_echo .= $form; 
            }
    
            $fields_echo .= '</div>'; //end field-data
            $fields_echo .= '</div>'; //end field
          }
        }

    Now, look at where I define the $fields_goals array.

    The first two array entries refer to fields ‘vision’ and ‘mission’ inside the group ‘ryit_user_legacy’ and the last three fields belong inside ‘ryit_user_goals’.

    When I define them with their alphanumeric names like this, the front end shows the values currently visible in the backend. But when I SAVE them, the values don’t change. Nothing gets saved.

    When I change to using the field KEYS however, I manage to save the values, but the values don’t show on the profile in the backend. In other words, the KEY sends the data SOMEWHERE, but it doesn’t show up on the profile.

    This seems like a bug to me, but I’m not sure.

    Everything works fine with fields which aren’t inside of groups. I’m stumped.

    Eivind

  • Going to be honest, by looking at the code you posted I don’t really understand what you are doing or why. I think that in order to grasp it fully I’d need to see it in action and be able to look at the field group(s)?

    Can you attach the JSON export for the field group(s) in question? Export them to JSON and then put them in a .zip file and you should be able to upload them here. Or if you want you could create a GIST or put the code online in some way so that I can see the field group(s)

  • Hi John, apologize for the late response.

    Here’s a short video about what I’m trying to do.

    https://vimeo.com/289890940/fab0aef3ce

    I think something may be wrong in the ACF core, but I don’t know. I may be making a mistake.

    Thank you so much!

    Eivind

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

You must be logged in to reply to this topic.