11

For example a filter form on admin areas can best be GET forms, that way they are bookmarkable and can filters be maintained across pager-pages.

How can one best use form_api for such forms? Will the _validate hooks be submitted? Can the _submit hook be omitted?

For example views-exposed filters work this way; But the code in views handling this is convoluted and complex, too complex to be used as good example for when developing custom interfaces with simple filter forms.

  • I don't understand the question. You can create or hook_alter any form to grab GET-data, if that's what you mean. – Letharion Jan 13 '12 at 12:06
  • I've updated the question with a reference to an examlple. – berkes Jan 13 '12 at 13:00
11

Use the #method parameter of the form like this:

<?php
  $form['#method'] = 'get';
?>
  • Sure. That is one part. But how about validation, submit hooks, and so on. Are the set? – berkes Jan 15 '12 at 20:50
  • 2
    A form's method must be set to post for it's submit and validate functions to be called implicitly. – Lester Peabody Nov 1 '13 at 19:31
  • Related: form_data keys always_process and method can also be set in the form definition which results in validate+process hooks being run. But I can't find an example of use for this and I still end up with form_state containing a method key set to post! – artfulrobot Dec 16 '14 at 16:05
4

Just in case anyone stumbles upon this question, this is a more complete real-life, yet very simple example :)

you will want to define the form

function YOURMODULE_search_form($form, &$form_state) {
  $form['#method'] = 'get';
  $form['#action'] = '/search';
  $form['#attributes'] = array('class' => array('mymod-search'));
  $form['search'] = array(
    '#type' => 'textfield',
    '#size' => 20,
  );
  return $form;
}

and then you can use it

$searchform = drupal_get_form('YOURMODULE_search_form');
$output .= drupal_render($searchform);

The last line could be i.e. a block['content'] or whatever you need.

  • For search forms, you may want to also put $form_state['redirect'] = false; in the submit handler to disable redirecting after the form is submitted. – Ali Gangji Sep 14 '14 at 16:15

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.