2

My situation:

I built a form with FAPI. I display the results in a table like this:

function grap_contact_repository_form_submit($form, &$form_state) {
  ...
  $form_state['results_table'] = array('#markup' => grap_contact_repository($matches));
  $form_state['rebuild'] = TRUE;
}

The grap_contact_repository function returns a theme_table.

My problem:

Because of the results_table that already uses a #markup type, I can't build a reset button for my form using '#type' => 'markup' too. (Because all the results table will be clickable too).

I'm looking for an alternative to make this button work. I tried this:

$form['clear'] = array(
  '#type'       => 'button',
  '#value'      => 'Réinitialiser',
  '#attributes' => array('onclick' => 'this.form.reset();'),
);

or this: (http://www.blue-bag.com/blog/drupal-form-api-reset-button)

  $form['reset'] = array(
    '#type' => 'button',
    '#button_type' => 'reset',
    '#value' => 'Réinitialiser',
    '#validate' => array(),
    '#attributes' => array(
      'onclick' => 'this.form.reset(); return false;',
    ),
  );

But it doesn't work. The onclick works if I put another JavaScript like alert. I have nothing in my JS errors console.

Does someone have an idea to make it work?

Thanks a lot :-)

2 Answers 2

1

Type should be reset, not button.

http://www.w3schools.com/tags/att_button_type.asp

You right, there is no such type. I was able to get this working with 2 submit buttons and return false (similar to your original implementation).

function drupalform_form($form, &$form_state) {

  $form['name']=array(
    '#type'=>'textfield',
    '#title'=>t('Enter your name'),
    '#description'=>t('Your first name goes here')
    );

  $form['submit_button1'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    );  

  $form['reset_button'] = array(
    '#type' => 'submit',    
    '#value' => 'Reset',
    '#validate' => array(),
    '#attributes' => array(
        'onclick' => 'this.form.reset(); return false;',
        ),
      );

  return $form;
}

If it still doesn't work there is this alternative solution: How to reset specific form elements

1
0

I found out that I had to do this in jQuery as I thought. But if I want all the form to be reset, means all the fields values to "", the .reset() method isn't enough. I had to set all the values to "" and empty my results table manually like this:

$form['reset'] = array(
'#type' => 'button',
'#button_type' => 'reset',
'#value' => 'Réinitialiser',
'#validate' => array(),
'#attributes' => array(
  'onclick' => 'jQuery("#edit-categories, #edit-contextes, #edit-organismes, #edit-fonctions").val(""); 
                jQuery(".history-results tr").remove();',
  ),
);

Your Answer

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

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