0

Hi I would like to create a custom Drupal form where there is a textfiled in which I input some data and it gets saved in variable table.

My requirement is the form should only contain Add , Remove and Add More button to add additional textfiled .

When the textfiled is empty it should show Add button adjacent to textfiled (when inputting data and clicking Add button it should save data in variable table ) ,when the textfiled is not empty it should show remove button adjacent to textfiled (when clicking Remove button it should remove data in variable table )

Clicking Add More button needs to create a empty textfiled with Add button adjacent to it.

All this need to happen without page reload.

I was able to achieve the task with Add , Remove , Add More and Submit Buttons.

I looked into the available resources and example modules but cant find helpful resource.

Can any one help me to modify the code so that the Submit Button can be removed.

My Code is as follows:

<?php
function mymodule_menu() {

    $items = array();

    $items['admin/mymodule'] = array(
        'title' => t('Example form'),
        'type' => MENU_CALLBACK,
        'page callback' => 'drupal_get_form',
        'page arguments' => array('mymodule_ajax_example_add_more'),
        'access callback' => TRUE,
    );

    return $items;
}

function mymodule_ajax_example_add_more($form, &$form_state) {

  $mymodule_ajax_events = variable_get('mymodule_ajax_example_content', array());
  $mymodule_ajax_events_count = count($mymodule_ajax_events);

  if ($mymodule_ajax_events_count == 0) {
    $mymodule_ajax_events_count = 1;
  }

  $form = array();
  $form['#tree'] = TRUE;
  $form['mymodule_ajax_events'] = [
    '#type' => 'container',
    '#weight' => 80,
    '#tree' => TRUE,
    // Set up the wrapper so that AJAX will be able to replace the fieldset.
    '#prefix' => '<div id="js-ajax-elements-wrapper">',
    '#suffix' => '</div>',
  ];
  $form_state['field_deltas'] = isset($form_state['field_deltas']) ? $form_state['field_deltas'] : range(0, $mymodule_ajax_events_count-1);

  $field_count = $form_state['field_deltas'];
  foreach ($field_count as $delta) {
    $form['mymodule_ajax_events'][$delta] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => ['container-inline'],
      ],
      '#tree' => TRUE,
    ];

    $form['mymodule_ajax_events'][$delta]['event_url'] = [
      '#type' => 'textfield',
      '#title' => t('Event URL' . ($delta + 1)),
      '#size' => 40,
      '#default_value' => (isset($mymodule_ajax_events[$delta])) ? $mymodule_ajax_events[$delta] : '',
    ];

    $form['mymodule_ajax_events'][$delta]['remove_event_url'] = array(
      '#type' => 'submit',
      '#value' => t('Remove'),
      '#submit' => ['mymodule_ajax_example_add_more_remove'],
      '#ajax' => [
        'callback' => 'mymodule_ajax_example_add_more_remove_callback',
        'wrapper' => 'js-ajax-elements-wrapper',
      ],
      '#weight' => 50,
      '#attributes' => [
        'class' => ['button-small'],
      ],
      '#name' => 'remove_event_url_' . $delta,
    );
  }

  $form['mymodule_ajax_events']['add_event_url'] = array(
    '#type' => 'submit',
    '#value' => t('Add one more'),
    '#submit' => ['mymodule_ajax_example_add_more_add_one'],
    '#ajax' => [
      'callback' => 'mymodule_ajax_example_add_more_add_one_callback',
      'wrapper' => 'js-ajax-elements-wrapper',
    ],
    '#weight' => 1,
  );
    $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 100,
  );

  return $form;
}

function mymodule_ajax_example_add_more_remove($form, &$form_state) {
  $delta_remove = $form_state['triggering_element']['#parents'][1];
  $k = array_search($delta_remove, $form_state['field_deltas']);
  unset($form_state['field_deltas'][$k]);

  $form_state['rebuild'] = TRUE;
  drupal_get_messages();
}

function mymodule_ajax_example_add_more_remove_callback($form, &$form_state) {
  return $form['mymodule_ajax_events'];
}

function mymodule_ajax_example_add_more_add_one($form, &$form_state) {
  $form_state['field_deltas'][] = count($form_state['field_deltas']) > 0 ? max($form_state['field_deltas']) + 1 : 0;
  //mymodule_ajax_example_add_more_validate();
  $form_state['rebuild'] = TRUE;
  // drupal_get_messages();
}

function mymodule_ajax_example_add_more_add_one_callback($form, $form_state) {
  return $form['mymodule_ajax_events'];
}

function mymodule_ajax_example_add_more_validate($form, $form_state) {
    dpm($form_state);
  foreach ($form_state['values']['mymodule_ajax_events'] as $key => $item) {
      //dpm($item);
      dpm($item['event_url']);
   if (!empty($item['event_url'])) {
      if (!preg_match('/https:\/\/example.com.*/i', $item['event_url'])) {
        form_set_error("mymodule_ajax_events][$key][event_url", 'Enter a valid Event URL.' . ($key + 1));
        //
      }else{
       drupal_set_message('URL ' . ($key + 1) . ' Created' );
   }
   }
 }
}

function mymodule_ajax_example_add_more_submit($form, $form_state) {
  $events_content = array();
  $events_count = 0;
  foreach ($form_state['values']['mymodule_ajax_events'] as $key => $item) {
    if (!empty($item['event_url'])) {
      $events_content[] = $item['event_url'];
      $events_count++;
   }
 }
  variable_set('mymodule_ajax_example_content', $events_content);
  variable_set('mymodule_ajax_example_count', $events_count);
}
0

If you want to omit the submit button altogether, you need to move the logic of the validate and submit to the ajax callbacks.

First, remove the submit button:

$form['submit'] = [
  '#type' => 'submit',
  '#value' => t('Submit'),
  '#weight' => 100,
];

Now you can take the logic from mymodule_ajax_example_add_more_validate and from mymodule_ajax_example_add_more_submit to the various ajax callbacks.

Each time the user interacts with the ajax buttons, you must validate the data and if all goes well, rewrite the variable to the database, because as far as we concern, this was the last interaction the user has with this form.

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.