I am using the add reapeatign filed proecess as ouotlined here

https://api.drupal.org/api/examples/ajax_example!ajax_example_graceful_d...

my group consists of 4 fields, a drop down, a textarea, a check box and a hidden "specify for other" field that is to be displayed when the "other" option is selected from the drop down.

it all works fine, except when i add a new field group the "hidden" field is displayed even though the options for the dropdown is not "other"

I need to keep the "specify for other" hidden except when the dropdown is other, any suggestions?

here is the relevent code section

//additional attributes
  $form['vertical_tabs_card_attributes']['attributes_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Additional Attributes '),
    // Set up the wrapper so that AJAX will be able to replace the fieldset.
    '#prefix' => '
	<div id="attributes-fieldset-wrapper">',
	'#suffix' => '</div>',
  );

  // Build the fieldset with the proper number of names. We'll use
  // $form_state['num_attrs'] to determine the number of textfields to build.

	if (empty($form_state['num_attrs'])) {
		$form_state['num_attrs'] = 1;
	}
	if(sizeof($credit_card['attributes']) > 1 && $form_state['num_attrs'] < sizeof($credit_card['attributes'])){
		$form_state['num_attrs'] = sizeof($credit_card['attributes']);
	}
  
  for ($i = 0; $i < $form_state['num_attrs']; $i++) {
	$form['vertical_tabs_card_attributes']['attributes_fieldset']['container'][$i] = array(
		'#type' => 'container',
		'#weight' => $i,
		'#attributes' => array(
							'class' => array(
							  'attribute-info',
							),
		
							),
	  );
    $form['vertical_tabs_card_attributes']['attributes_fieldset']['container'][$i]['attribute_id'] = array(
      '#type' => 'select',
      '#title' => t('Attribute'),
	  '#options' => $select + $options['attributes'] + array('-999' => 'Other'),
	  '#default_value' => $credit_card['attributes'][$i]->attribute_id,

    );
	
	
	
	
	$form['vertical_tabs_card_attributes']['attributes_fieldset']['container'][$i]['attribute_detail'] = array(
      '#type' => 'textfield',
      '#title' => t('Attribute Description'),
	  '#default_value' => $credit_card['attributes'][$i]->attribute_details,
    );
	$form['vertical_tabs_card_attributes']['attributes_fieldset']['container'][$i]['show_above_card'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show This content above Card Name on display:'),
	  '#default_value' => $credit_card['attributes'][$i]->show_above_card,
    );
	$form['vertical_tabs_card_attributes']['attributes_fieldset']['container'][$i]['other_attribute'] = array(
    '#type' => 'textfield',
    '#title' => t('Specify for Other:'),
	'#description' => 'Adding a value here will also add it to the Attribute Dropdown options when this this card is saved', 
    '#attributes' => array('style' => 'float:left; max-width:100px; clear:both;'),
    '#states' => array(
			'visible' => array(   // action to take.
				"#edit-vertical-tabs-card-attributes-attributes-fieldset-container-$i-attribute-id" => array('value' => '-999'),
			),
		),
	);
  }
  $form['vertical_tabs_card_attributes']['attributes_fieldset']['add_attr'] = array(
    '#type' => 'submit',
    '#value' => t('Add Attribute'),
    '#submit' => array('credit_card_managment_form_add_one'),
    // See the examples in credit_card_managment_form.module for more details on the
    // properties of #ajax.
	
    '#ajax' => array(
      'callback' => 'credit_card_managment_form_callback',
      'wrapper' => 'attributes-fieldset-wrapper',
    ),
  );
  if ($form_state['num_attrs'] > 1) {
    $form['vertical_tabs_card_attributes']['attributes_fieldset']['remove_attr'] = array(
      '#type' => 'submit',
      '#value' => t('Remove Last Attribute'),
      '#submit' => array('credit_card_managment_form_remove_one'),
      '#ajax' => array(
        'callback' => 'credit_card_managment_form_callback',
        'wrapper' => 'attributes-fieldset-wrapper',
      ),
    );
  }

Comments

Jaypan’s picture

You can use the #states attribute of the Form API to do this.

jesterFortyEight’s picture

I have implmented states, the field is hidden on initial load, but when i click to add new field group, all the fields that should be hidden are displayed

initial state - form load

$form['vertical_tabs_card_attributes']['attributes_fieldset']['container'][$i]['other_attribute'] = array(
    '#type' => 'textfield',
    '#title' => t('Specify for Other:'),
	'#description' => 'Adding a value here will also add it to the Attribute Dropdown options when this this card is saved', 
    '#attributes' => array('style' => 'float:left; max-width:100px; clear:both;'),
    '#states' => array(
			'visible' => array(   // action to take.
				"#edit-vertical-tabs-card-attributes-attributes-fieldset-container-$i-attribute-id" => array('value' => '-999'),
			),
			
		),
	);

the field is hidden, on load and shows/hides when the dropdown options changed accordingly.

but when i use it in a repeating field group the state is not triggered on refresh/form rebuild, the "hidden" field shows no matter what, it seems that the jquery is not attached to on/live conditions so does not trigger, i tried using attribute and a style of display none, but that only hid the input not the label/description