Support

Account

Home Forums Add-ons Repeater Field Pre-populate checkbox choices per repeater row

Solved

Pre-populate checkbox choices per repeater row

  • Hi there,

    I was wundering if the following is possible;

    1) I have a repeater in my options page to add questions to a form. Each row has a label and another repeater containing every possible answer.

    2) In one of my field groups I want to pre-populate a repeater field with rows for each question from this options page, containing:

    – a checkbox field containing all the answers for that question as choices and the question label as the checkbox field label.

    The problem is that if I use this method:
    https://www.advancedcustomfields.com/resources/acf-load_field/
    it will populate the choices for each row, but I need them to be unique on each row.

    Is this even possible or is it necessary to already have a unique field for each question in my field group in order to populate the choices uniquely?

    Thanks a lot!

  • You cannot do this using PHP. To do this you would need to write the values to a JavaScript variable on the page and then use the ACF JS API to populate the values of each row. There are no examples that I know of for this.

  • Ok thanks for your reply!

    I thought I could use the acf_add_local_field instead for this, so I added the following action that displays my checkboxes correctly now:

    add_action('acf/init',	array(&$this, 'addServiceMatchMakerFilter'));
    public function addServiceMatchMakerFilter() {
    	if (function_exists('acf_add_local_field')) {
    		$posts     = new Posts();
    		$global    = $posts->getGlobalInfo();
    		$questions = $global['match_maker_questions'];
    
    		// Add field for each match maker question
    		if (!empty($questions)) {
    			foreach ($questions as $question) {
    				$choices = [];
    
    				if (!empty($question['choices'])) {
    					foreach ($question['choices'] as $choice) {
    						$value           = $choice['choice_value'] ? $choice['choice_value'] : $choice['choice_label'];
    						$choices[$value] = $choice['choice_label'];
    					}
    				}
    
    				acf_add_local_field([
    					'key'          => 'field_' . uniqid(),
    					'label'        => $question['label'],
    					'name'         => sanitize_title($question['label']),
    					'type'         => 'checkbox',
    					'parent'       => 'field_5d3ebbd2fef0c',
    					'choices'      => $choices,
    					//'layout'       => 'horizontal',
    				]);
    			}
    		}
    	}
    }

    However, when saving my post the values from these fields are empty again (also in frontend). Any idea why this is happening?

    Thanks, Toine

  • Ah duh! This line messed it up:
    'key' => 'field_' . uniqid()

    This is different on every page load…
    Is there a best practice on how many characters this key should be?

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

The topic ‘Pre-populate checkbox choices per repeater row’ is closed to new replies.