Hi all,

I have a form that can have multiple instances of a field group which includes 2 dropdowns, the second dependent of the first. Inside each group, the first dropdown (independent) lists Labels and the second (dependent) lists Values Each label can have multiple values in the database, but each value can only belong to a single label.

While creating an Entry, I am using AJAX to correctly re-populate each dependent dropdown according to the value of the adjastent independent field (Labels) and it works just fine, saving the values to the related tables.

However, while editing an Entry, I would like each field in the form to be pre-populated with the correct values before the option is selected (Each dropdown should only include values that belong to the label selected). So the end result should be multiple groups with the correct value selected and only the relevant options in each dependent dropdown.

Here is some code... The fields in the form:

$this->add(new Select('label_id[]', AttributeLabels::find(array( 'conditions' => 'user_id = :user:', 'bind' => array('user' => $this->auth->getUser()->id) )), array(
  'using' => array(
      'id',
      'label',
  ),
  'useEmpty' => true,
  'emptyText' => '...',
  'emptyValue' => '',
  'class' => 'form-control validate-required',
  'id' => 'label_id'
)));

$this->add(new Select('value_id[]', $values, array(
    'using' => array(
        'id',
        'value',
    ),
    'useEmpty' => true,
    'emptyText' => '...',
    'emptyValue' => '',
    'class' => 'form-control validate-required',
    'id' => 'value_id'
  )));

$values just comes from an SQL query and this is where I need help. Since I can have multiple label_id[] and value_id[] fields (generated in a foreach loop according to the total number of label/value sets), I want to be able to use the current label_id from the loop to change the SQL query that creates the $values.

Here is how I am creating each field in the foreach loop (using Volt):

{{ form.render("label_id[]", [ 'value': attribute.label_id ]) }}
{{ form.render("value_id[]", [ 'value': attribute.value_id ]) }}

I have already tried to use $entity in the form, but this gives me all the label_id values and not the only currently being populated.

Any ideas?