views:

555

answers:

2

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?

A: 

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.

googletorp
The plan is to add an AHAH callback to the button so I can fire off a query to Wikipedia in the background and replace the body field with text after the user hits search.CCK manages to do this with its Add More buttons so I guess I'll keep digging back there. Thanks anyway. I may use jQUery etc as a last resort.
kidrobot
A: 

You could use jQuery's event.preventDefault()

http://api.jquery.com/event.preventDefault/

Marlorn

related questions