0

I have an array...

    $my_array = array(1 => 'Value One', 2 => 'Value Two',3 => 'Value Three');

In Form API, I want to create checkbox for each value.

    foreach($my_array as $key => $value) {
      $form['custom_field'] = array(
        '#type' => 'checkbox',
        '#title' => $value,
        '#default_value' => variable_get('custom_field'),
      );
    }

by default is stores in value, '0' or '1'. It should actuly be saving the $key as value.

I know this can be done simply using 'type' as 'checkboxes' but actuly I need to goroup it each time each check box with a text field.

    foreach($my_array as $key => $value) {
      $form['custom_field'.$key] = array(
          '#type' => 'checkbox',
          '#title' => $value,
          '#default_value' => variable_get('custom_field'),
        );
        $form['custom_field'.$key] = array(
          '#type' => 'textfield',
          '#title' => 'Limit',
          '#size' => 10,
          '#maxlength' => 10,
          '#default_value' => variable_get('limit_variable'),
          '#required' => TRUE,
        );
      }

` Any help?

2

You can try adding '#return_value':

foreach($my_array as $key => $value) {
      $form['custom_field'] = array(
        '#type' => 'checkbox',
        '#return_value' => $key,
        '#title' => $value,
        '#default_value' => variable_get('custom_field'),
      );
    }
1
  • 2
    @Tanvir if it worked, why don't you mark it as accepted? – Mołot Oct 6 '14 at 20:01
0

You can use Fieldset to group fields together. You cannot use same key for two form elements since they override existing. So, changed checkbox key value to 'custom_field_checkbox_'.$key and text field to 'custom_field_text_'.$key

foreach($my_array as $key => $value) {
        $form['custom_field_group_'.$key] = array(
          '#type' => 'fieldset',
          '#title' => t('Contact settings'), 
          '#collapsible' => TRUE, 
          '#collapsed' => FALSE,
        );
        $form['custom_field_group_'.$key]['custom_field_checkbox_'.$key] = array(
          '#type' => 'checkbox',
          '#title' => $value,
          '#return_value' => $key,
          '#default_value' => variable_get('custom_field'),
        );
        $form['custom_field_group_'.$key]['custom_field_text_'.$key] = array(
          '#type' => 'textfield',
          '#title' => 'Limit',
          '#size' => 10,
          '#maxlength' => 10,
          '#default_value' => variable_get('limit_variable'),
          '#required' => TRUE,
        );
      }
3
  • Thanks Anil, fieldName is just a typo. But I should have take care of that even if I am just posting to query something. – TanvirAhmad Feb 27 '14 at 16:08
  • Still, I am not getting what I need. Solution you suggested will still save 'checkbox' as 1 or 0. e-g <input type="checkbox" class="form-checkbox" value="1" name="custom_field_checkbox_5" id="edit-custom-field-checkbox-5"> - You see here is value=1, but it should be the key of the value. – TanvirAhmad Feb 27 '14 at 16:09
  • Updated answer... – Anil Sagar Feb 28 '14 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.