R – Stop a custom submit button from firing the form validation on a CCK form

cckdrupaldrupal-alterdrupal-fapi

I've added a submit button inside a fieldgroup on a CCK form using hook_form_alter as follows:

function mymodule_form_alter(&$form, $form_state, $form_id) {

  if ($form_id == 'object_node_form') {

   $form['group_wikipedia']['search'] = array(

 '#type' => 'submit',
 '#value' => t('Search Wikipedia'),
 '#name' => 'searchwiki',
 '#submit' => array('mymodule_searchwiki_submit'),
   );

  }
}

When I press the button, the validation handlers for the full form eg. checks for required fields, run as though I have pressed the 'Submit' button at the end of the form.

I thought that changing the #name property from 'op' to 'searchwiki' would prevent this kind of mix-up, but not so.

Does anyone know a workaround for this?

Best Answer

I believe that the entire form will always be submitted no matter which submit button the user presses. If you want to make a search function, what you could do, is make some AJAX that fetch the data and display it. Using jQuery you would also be able to stop the form submit, but returning FALSE on the button click function (that you can make in your js). You probably wont be able to use the goodness of the Drupal form api though.

Related Topic