'Example form ahah', 'page callback' => 'drupal_get_form', 'page arguments' => array('ahah_example_form_form'), 'access arguments' => array('access content'), 'type' => MENU_NORMAL_ITEM, ); $items['form_ahah/js'] = array( 'title' => 'Javascript Choice Form', 'page callback' => 'ahah_example_form_js', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implementacion del hook_form */ function ahah_example_form_form($form_state) { $cnt_fieldset = isset($form_state['values']['cnt_fieldset']) ? $form_state['values']['cnt_fieldset'] : 0; $form['cnt_fieldset'] = array( '#type' => 'hidden', '#value' => $cnt_fieldset, ); $form["fieldset"] = array( '#type' => "fieldset", '#title' => t("Contact settings"), '#weight' => 0, '#collapsible' => TRUE, '#collapsed' => FALSE, '#prefix' => '
', '#suffix' => '
', ); for ($i=0; $i<=$cnt_fieldset; $i++) { $form["fieldset"]["selected_$i"] = array( '#type' => 'select', '#title' => t("Selected $i"), '#default_value' => $form_state['values']["selected_$i"], '#options' => array( 0 => t('No'), 1 => t('Yes'), ), '#description' => t('Set this to Yes if you would like this category to be selected by default.'), ); } $form['submit_ahah'] = array( '#type' => 'submit', '#value' => t('Add'), '#weight' => 1, '#attributes' => array('class' => 'add-buttom', 'title' => t('Click here to add more tabs.')), '#ahah' => array( 'path' => 'form_ahah/js', 'wrapper' => 'wrapper-fieldset', 'method' => 'replace', 'effect' => 'fade', ), ); $form['submit_form'] = array( '#type' => 'submit', '#value' => t('Send'), '#weight' => 2, ); return $form; } /** * Implementation ahah in form */ function ahah_example_form_js() { $form_state = array( 'storage' => NULL, 'submitted' => FALSE, 'values' => $_POST, ); $form_build_id = $_POST['form_build_id']; // Get the form from the cache. $form = form_get_cache($form_build_id, $form_state); $args = $form['#parameters']; $form_id = array_shift($args); // We will run some of the submit handlers so we need to disable redirecting. $form['#redirect'] = FALSE; // We need to process the form, prepare for that by setting a few internals // variables. $form['#post'] = $_POST; $form['#programmed'] = FALSE; $form_state['post'] = $_POST; // Build, validate and if possible, submit the form. drupal_process_form($form_id, $form, $form_state); // cantidad de campo $form_state['values']['cnt_fieldset'] = $form_state['values']['cnt_fieldset']+1; // This call recreates the form relying solely on the form_state that the // drupal_process_form set up. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); $new_form = $form['fieldset']; //modificando el valor del campo oculto unset($new_form['#prefix'], $new_form['#suffix']); // Prevent duplicate wrappers. $output = theme('status_messages') . drupal_render($new_form); drupal_json(array('status' => TRUE, 'data' => $output)); } /** * validando las entradas */ function ahah_example_form_form_validate($form, &$form_state) { if ($form_state['clicked_button']['#id'] == 'edit-submit-form') { $cantidad = $form_state['values']['cnt_fieldset']; for ($i=0; $i<=$cantidad; $i++) { if ($form_state['values']["selected_$i"] == 0) { form_set_error("selected_$i", t("este campo es obligatorio")); } } } } /** * enviando las entradas */ function ahah_example_form_form_submit($form, &$form_state) { if ($form_state['clicked_button']['#id'] == 'edit-submit-form') { drupal_set_message(t("se enviaron los datos")); drupal_goto('example/form1'); } }