I have created a module and a form. The form is presented properly, including the data from the records it manages (using the xxx_load function and URL wildcards). The form validate function gets called properly. But my submit function is not called. I do have a submit element in the form, but in the HTML rendered page it shows up as a submit-type button with the name 'op'. My suspicion is that this is the reason my submit function is not called. Furthermore, when I press the submit button, the same form is shown again (with changed data), even if I add a 'redirect' element to my form. There are no error messages, not on screen, nor in the reports.

Using D6.9

What have I already ruled out:
- Nowhere in my own modules or in the template.php do I execute a hook_form_alter() on this form.
- The function names are proper and the parameter lists are as is described in the FAPI 6.x documentation

I have searched the web extensively, but cannot resolve my issue. Can anyone point me in the right direction?

Thanx.

Comments

jaypan’s picture

Probably something wrong with your code.

Contact me to contract me for D7 -> D10/11 migrations.

phkemper’s picture

OK, some more debug background:
- The entire _validate() function get's executed and returns properly
- The _submit() function not called at all. Even a print statement as the very first statement in the function, does not get executed.

brandon.dixon’s picture

Can you post your submit function or part of it? The redirection is surely happening because the submit is not being called. If the submit function is no defined or coded then it will just refresh the page and input the default values (could explain the form values changing?).

phkemper’s picture

Here are the code snippets:

/* One of the menu items */
function onder_een_dak_central_menu()
{
  return array(
    'admin/settings/onder-een-dak/%onder_een_dak_site/edit' => array(
      'title' => 'Edit web site',
      'page callback' => 'drupal_get_form',
      'page arguments' => array( 'onder_een_dak_admin_edit_form', 3 ),
      'access arguments' => array( 'administer site configuration' ),
      'description' => 'Edit web site',
      'type' => MENU_CALLBACK,
    ),
  );
}

/* The load function */
function onder_een_dak_site_load( $sid )
{
  return db_fetch_array(db_query('SELECT * FROM {oed_sites} WHERE sid=%d', $sid ));
}

/* Part of the form function */
function onder_een_dak_admin_edit_form( &$form_state, $site = false )
{
  // Parts left out
  return array(
    'sid' => array(
      '#type' => 'hidden',
      '#value' => $form_state['values']['sid'],
    ),
    'type' => array(
      '#type' => 'radios',
      '#title' => t('Type of site'),
      '#default_value' => $form_state['values']['type'],
      '#options' => array( 'vak' => 'Vak site', 'locatie' => 'Locatie site' ),
      '#required' => true
    ),
    // Parts left out
    'submit' => array(
      '#type' => 'submit',
      '#value' => $site['sid'] ? t('update') : t('add')
    ),
    'back' => array(
      '#value' => l(t('cancel'),'admin/settings/onder-een-dak')
    )
  );
}

/* Validation function (which works) */
function onder_een_dak_admin_edit_form_validate( $form, &$form_state )
{
}

/* Submit function */
function onder_een_dak_admin_edit_form_submit( $form, &$form_state )
{
  if ( $form_state['values']['sid'] == 0 )
  {
    // Add the new site
    $result = db_query( "INSERT INTO {oed_sites} (type,url,name,letter,user,pass,updated) VALUES ('%s','%s','%s','%s','%s','%s','0000-00-00 00:00:00')", $form_state['values']['type'],$form_state['values']['url'],$form_state['values']['name'],strtolower(substr($form_state['values']['letter'],0,1)),$form_state['values']['user'], md5($form_state['values']['pass']) );
  }
  else
  {
    // Update existing site
    $result = db_query( "UPDATE {oed_sites} SET type='%s', url='%s', name='%s', letter='%s', user='%s', pass='%s' WHERE sid=%d", $form_state['values']['type'], $form_state['values']['url'], $form_state['values']['name'], strtolower(substr($form_state['values']['letter'],0,1)), $form_state['values']['user'], md5($form_state['values']['pass']), $form_state['values']['sid'] );
  }
  if ( !$result )
  {
    drupal_set_message( t('Could not add/update the site into the database') );
  }
  // I Also tried the 'redirect' property in the form
  drupal_goto( 'admin/settings/onder-een-dak/' );
}

brandon.dixon’s picture

Try modifying the name of the submit function to MODULE_NAME_SUBMIT or in your case onder_een_dak_submit(). Right now you have some additional information between the hook and the module itself. I think this may be why you are not seeing it execute.

heine’s picture

This is not a node form, hook_submit is totally inappropriate.

jaypan’s picture

What you have there looks fine. Why don't you put the rest of your form definition up.

Contact me to contract me for D7 -> D10/11 migrations.

phkemper’s picture

Hi guys, here's the whole function:

/**
 * Edit a single site record
 *
 * If there is no site record, assume a create request
 *
 * @param   mixed[]         Form state
 * @param   midex[string]   Site record from sites table (optional)
 * @return  mixed[string]   Edit form
 */
function onder_een_dak_admin_edit_form( &$form_state, $site = false )
{
  if ( $site === false )
  {
    $site = array( 'sid' => 0, 'type' => 'vak', 'url' => 'http://', 'name' => '', 'letter' => 'x', 'user' => '', 'pass' => '', 'updated' => '0000-00-00 00:00:00' );
  }
  if ( $form_state['values']['sid'] == 0 )
  {
    $form_state['values']['sid'] = $site['sid'];
    $form_state['values']['type'] = $site['type'];
    $form_state['values']['url'] = $site['url'];
    $form_state['values']['name'] = $site['name'];
    $form_state['values']['letter'] = $site['letter'];
    $form_state['values']['user'] = $site['user'];
    $form_state['values']['pass'] = '';
  }
  return array(
    'sid' => array(
      '#type' => 'hidden',
      '#value' => $form_state['values']['sid'],
    ),
    'type' => array(
      '#type' => 'radios',
      '#title' => t('Type of site'),
      '#default_value' => $form_state['values']['type'],
      '#options' => array( 'vak' => 'Vak site', 'locatie' => 'Locatie site' ),
      '#required' => true
    ),
    'url' => array(
      '#type' => 'textfield',
      '#title' => t('Web site address'),
      '#default_value' => $form_state['values']['url'],
      '#description' => t('The URL of the site\'s home page'),
      '#required' => true
    ),
    'name' => array(
      '#type' => 'textfield',
      '#title' => t('Name of the site'),
      '#default_value' => $form_state['values']['name'],
      '#required' => true
    ),
    'letter' => array(
      '#type' => 'textfield',
      '#title' => t('Code letter of the site'),
      '#default_value' => $form_state['values']['letter'],
      '#required' => true
    ),
    'user' => array(
      '#type' => 'textfield',
      '#title' => t('Login ID'),
      '#default_value' => $form_state['values']['user'],
      '#description' => t('Choose one of the registered site users. I.e. the administrator or web master'),
      '#required' => true
    ),
    'pass' => array(
      '#type' => 'password',
      '#title' => t('Password'),
      '#default_value' => $form_state['values']['user'],
      '#description' => t('Use the password associated with the Login ID'),
      '#required' => true
    ),
    'submit' => array(
      '#type' => 'submit',
      '#value' => $site['sid'] ? t('update') : t('add')
    ),
    'back' => array(
      '#value' => l(t('cancel'),'admin/settings/onder-een-dak')
    )
  );
}
phkemper’s picture

OK, I have solved the issue (but in unearthed another one). Here's the what and the how.

It turns out that my validate function sets and error (with form_set_error() ). The only thing is, the error was not showing at the top of my form.

The way I found out: I installed xdebug and traced the execution logic. There I found out that form_set_error() was called. From there it was easy to find which form_set_error() was actually called and I could update the program logic in the validate function.

Thanks to all for chipping in.

jaypan’s picture

Glad to hear you found it.

Sounds like $messages is not being outputted in your theme's page.tpl.php. Either that or you are overwriting them somehow in your theme's template.php.

Contact me to contract me for D7 -> D10/11 migrations.

brandon.dixon’s picture

Try using the submit hook for your form. Doing so will cause the code within that function to be executed.

http://drupal.org/node/58689

phkemper’s picture

I tried the submit property in the form (although the 6 version of course). No luck.

heine’s picture

Please post your code. We cannot help you otherwise.

haggins’s picture

Hi phkemper,
I've got exactly the same problem as you had. I installed xdebug and got it running with eclipse. But I don't know how to use it. By pressing F11 Firefox opens my project and Eclipse opens the PHP Debug perspective.
Can you tell me where you saw which functions get called? And where I should set a breakpoint (at the validate function or somewhere else)? I'm a little lost as you can see ;)

Thanks for your help!

Greetz
haggins

kentendo’s picture

I have the same problem, except neither validate or submit get called when I use the form element submit like below. Am I doing something stupid?

I have defined a page called testme in hook_menu below, which returns my form. Also..I'm using "Drupal for Firebug" module so that's what these firep()'s are

<?php

function testme_menu() {
    $items['testme'] = array(
        'title' => 'Upgrade Page',
        'page callback' => 'testme_page',
        'access callback' => TRUE,
    );
    return $items;
}

function testme_page() {
    return drupal_get_form('testme_form');
}

function testme_form($form_state) {
    $form = array();
    $form['membership'] = array(
        '#type' => 'radios',
        '#title' => t('Choose a Membership'),
        '#default_value' => '0',
        '#options' => array(0, 1, 2),
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );

    // the only way to get validate to run is to name the submit button something like billy
    // but still, the default submit function doesn't run
    //$form['billy'] = array('#type' => 'button', '#value' => t('Upgrade'));

    // tried adding custom submit/validate functions
    //$form['#submit'][] = 'testme_form_custom_submit';
    //$form['#validate'][] = 'testme_form_custom_validate';
    return $form;
}


function testme_form_validate(&$form, &$form_state) {
    firep($form, 'testme_upgrade_form_validate');
}

function testme_form_submit($form, &$form_state) {
    firep($form, 'testme_upgrade_form_submit');
}

/*
 * I couldn't get these to work either
 function testme_form_custom_validate(&$form, &$form_state) {
     firep('testme_upgrade_form_validate');
 }

 function testme_form_custom_submit($form, &$form_state) {
     firep('testme_upgrade_form_submit');
 }
 */
teflo’s picture

Same problem

function reservations_form(&$form_state){
  
  if(is_numeric(arg(1))){

	  $node_object = node_load(arg(1));
	  
	  if($node_object->type == "object"){
	  	
	  	$granularity = $node_object->granularity;
		
	  	$format = 'd-m-Y';
	
		$date = date($format);
		
		$format_hour = 'H:i';
		
		$hour = date($format_hour);

	  	switch($granularity){
	  		case 'halfhour':
	  			
				  $form['day_start'] = array(
				     '#type' => 'date_select', // types 'date_text' and 'date_timezone' are also supported. See .inc file.
				     '#title' => 'From date',
				     '#default_value' => $date,
				     '#date_format' => $format,
				     '#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
				     '#date_increment' => 30, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
				     '#date_year_range' => '0:+2', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
				  	 '#required' => TRUE,
				  );
				  
				  $form['more_days'] = array(
					 '#type' =>'checkbox', 
					 '#default_value' => 0,
					 '#title' => t('More days'),	
					);
				  
				  $form['day_finish'] = array	(
				     '#type' => 'date_select', // types 'date_text' and 'date_timezone' are also supported. See .inc file.
				     '#title' => 'To date',
				     '#default_value' => $date,
				     '#date_format' => $format,
				     '#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
				     '#date_increment' => 30, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
				     '#date_year_range' => '0:+2', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
				  );				  			  
	  			
			
				  $form['submit'] = array(
					  '#type' => 'submit',
					  '#value' => t('save'),

				  );
	  			break;
	  	}	  			

	  	
	  }
  	
  }
  
  $form['#theme'] = 'reservations_form';


  return $form;
}

function reservations_form_submit($form, &$form_state) {
	die("submit");
}