2

So I'm trying to create a form with a validate and submit function, here is my code :

the form :

    function custom_form() {
      $form = array();
      $form['field1'] = array(
        '#type' => 'textfield',
        '#title' => t('Field1'),
        '#required' => TRUE
      );
      $form['field2'] = array(
        '#type' => 'textfield',
        '#title' => t('Field2'),
        '#required' => TRUE
      );
      $form['field3'] = array(
        '#type' => 'textfield',
        '#title' => t('Field3'),
        '#required' => TRUE
      );
      $form['submit'] = array(
        '#type'=>'submit',
        '#value'=>t('Submit')
      );
      return $form;
    }

the validate function :

    function custom_form_validate($form, &$form_state) {
      if(empty($form_state['values']['field1']))
        form_set_error('field1','field1 is empty.');
      else if(empty($form_state['values']['field2']))
        form_set_error('field2','field2 is empty.');
      else if(empty($form_state['values']['field3']))
         form_set_error('field3','field3 is empty.');
    }

and the submit function :

    function custom_form_submit($form, &$form_state) {
       drupal_set_message("Form has been submitted");
    }

But when I click on submit nothing appends, do I forget something ?

EDIT : Here is all my code (I'm using shortcode module to create a custom shortcode in order to show my form)

    function custom_form_info() {
      $shortcodes['form'] = array(
        'title' => t('Link form'),
        'description' => t('A simple form.'),
        'process callback' => 'custom_shortcode_form',
      );
      return $shortcodes;
    }

    function custom_shortcode_form($attrs, $text) {
      $form = drupal_get_form('custom_form');
      $attrs = drupal_render_children($form);
      return $attrs;
    }
0

Add the following lines to your custom_form function:

$form['#validate'][] = 'custom_form_validate';
$form['#submit'][] = 'custom_form_submit';

Clear the cache afterwards and you will be good

5
  • 2
    Technically these are not needed, as Drupal looks for your_form_validate() and your_form_submit() by default... BUT it's hard to tell in this case as @Barbrousse is not including the rest of the code – argiepiano Dec 9 '15 at 19:12
  • 1
    @argiepiano agreed. but it only works when you have followed the proper names of the hook in the module. this will make sure that your validate and submit will get fired regardless of anything. – Shabir A. Dec 9 '15 at 19:29
  • Hi thanks for your responses, I've tried to add those two lines and still nothing, I'm going to edit my first post to include all my code. – Barbrousse Dec 9 '15 at 20:39
  • Why are you using hook_info() or its just a callback function defined in hook_menu? – Shabir A. Dec 9 '15 at 20:50
  • To be honest in order to create a shortcode I've follow this tuto, but the shorcodes include in the shortcode module aren't using hook_menu but hook_info. – Barbrousse Dec 9 '15 at 22:07
0

Reading your comments, you mentioned you are using the shortcode module to create this simple form. I'm not familiar with this module, but, honestly, I think it's just easier to follow the "traditional" way of creating forms in Drupal explained in many tutorials, including this one, which uses hook_menu() in a custom module to create a path to the form. All your code in your first post will work just fine - the only difference is that, instead of using custom_form_info() you would use something like this.

Be sure to name the implementation of hook_menu() with the name of your module (in this case below, the module would be called my_module.module).

function my_module_menu() {
 $items['path-to-my-form'] = array( 
    'title' => 'Example Form', //page title
    'description' => 'My form.',
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('custom_form'),
    'access callback' => TRUE
  );

  return $items;
}

Include the rest of your functions (custom_form() and the submit and validate handlers) in the same module.

To visit your form follow www.example.com/path-to-my-form

1
  • Hi @argiepiano thanks for your help, I know that using hook_menu would be really easier but a really need to use shortcode :/ . – Barbrousse Dec 10 '15 at 12:58

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.