I'm building a form for a modules configuration page. The last line of the form callback is the following:

return system_settings_from($form);

I want to add a textfield to the form, but I want to be able to enter multiple entires into the field using the same widget you get when manually creating a field of this type (where cardinality > 1) where you get the text field with an "add another item" button.

How can I use that type of widget in a form for a module configuration page?

  • The multi-value widget is implemented specifically for the field API, for fields attached to entities. It's not available for form fields in general. So you'll probably need to roll your own solution. If I was going to solve this I'd probably code a form with a single textarea then use jQuery to turn it into a series of textfields, one per row, with buttons to add/delete entries. Sep 19, 2014 at 12:52

1 Answer 1


You won't be able to use the multi-value functionality used in the Field API in your custom forms.

However, with the Form API and Ajax you should be able to create dynamic form fields. See Ajax Example in the Examples for Developers project.

 * Form manipulation through AJAX.
function ajax_example_autocheckboxes($form, &$form_state) {

  // Since the form builder is called after every AJAX request, we rebuild
  // the form based on $form_state.
  $num_checkboxes = !empty($form_state['values']['howmany_select']) ? $form_state['values']['howmany_select'] : 1;

  $form['howmany_select'] = array(
    '#title' => t('How many checkboxes do you want?'),
    '#type' => 'select',
    '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4),
    '#default_value' => $num_checkboxes,
    '#ajax' => array(
      'callback' => 'ajax_example_autocheckboxes_callback',
      'wrapper' => 'checkboxes-div',
      'effect' => 'slide',


  for ($i = 1; $i <= $num_checkboxes; $i++) {
    $form['checkboxes_fieldset']["checkbox$i"] = array(
      '#type' => 'checkbox',
      '#title' => "Checkbox $i",


  return $form;

 * Callback for autocheckboxes.
 * @return array
 *   Renderable array (the checkboxes fieldset)
function ajax_example_autocheckboxes_callback($form, $form_state) {
  return $form['checkboxes_fieldset'];

Another, quick solution that may work (or not) for your case is using an entity (e.g. a single node of a new content type) to store that configuration.

  • I already did this. My requirement is need to make the same in admin settings form.
    – Cool
    Sep 24, 2014 at 11:16
  • 1
    @ARUN That's the point - you'll need to code it yourself, Drupal doesn't provide you with the functionality you're looking for.
    – Clive
    Sep 25, 2014 at 12:25
  • How can we do the same in D8?
    – Cool
    Jul 22, 2017 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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