Hello,

I am using version 6.10 and have created a form on one of my pages, but I have the following issues for which the tutorial and documentation that I could find do not help:

1) After creating my _form method and _page method, what am I supposed to do get the form to be shown? I am currently calling the print method on the results of _page method, but the tutorial mentions nothing of this or something else that must be done. The call to print shows the form correctly.
2) How are the _validate and _submit methods called? When I press my submit button, neither is called. I am assuming the naming convention of all the methods is so that callbacks can be called.
3) I am getting the following error out and was wondering if any fix existed for it or if it is the source of the above issues.
Invalid argument supplied for foreach() in /var/www/drupal/includes/form.inc on line 1197.

I can post the code if necessary...

Jason

Comments

nevets’s picture

A link to the tutorial would help to understand the approach you have taken.

It sounds though instead of creating a module you have placed the code in the body field of some content type.

lonerzzz’s picture

nevets’s picture

Are you creating a module (the code you reference belongs in a module).

lonerzzz’s picture

Can you describe how or point me to the docs that describe how to package the form as a module. I am going only by the tutorial mentioned and it gives me no references to placing the form in a module.

jaypan’s picture

Here is a very simple example of how you can create a form in a node:

<?php

echo drupal_get_form('my_form'); // this prints the form to the page

//next, we will create the form that will be printed with the above code
function my_form($form)
{
  $form['item1'] = array
  (
    '#type' => 'textfield',
    '#title' => 'Field 1',
  );
  $form['submit'] = array
  (
    '#type' => 'submit',
    '#value' => 'submit',
  );
  return $form;
}

//next we create our validation function. This will automatically be called, since the name of the function is the same as the name of the form function, with _validate appended to it

function my_form_validate($form, $form_state)
{
  //validate form here
}

//and finally we create our submit function
function my_form_submit($form, $form_state)
{
  //do submission stuff here
}
?>

You will need to install the php_filter module, and use the php_filter input filter in order for this to work. If you are doing it in a module, then the method will be slightly different from what I have shown.

note: code not tested, so there may be typos.

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

lonerzzz’s picture

Thanks a bunch for the example. My code is pretty much as you show and the form does get printed out so the question then arises as to why the _submit and _validate methods do not get called. The names of the methods are:

workshopbooking_form
workshopbooking_form_validate
workshopbooking_form_submit

and this would appear to follow the naming convention so I am a bit confused. Suggestions?

As per the last comments in your post and in the other post, does this php code have to be submitted specially? I am simply submitting it as the body of my page and then modifying the page attributes from the same edit page to indicate that php processing of the page is allowed.

jaypan’s picture

Lets see your code.

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

lonerzzz’s picture

Attached is the code for which the _submit and _validate methods are not working. The second set of eyes is MUCH appreciated.

<?php

function workshopbooking_form($form_state)
{
	$form = array();
	// Workshop Selection
	$form['workshop'] = array(
		'#type' => 'fieldset',
		'#title' => t('Workshop Selection'),
		'#tree' => TRUE,
	);
	$workshops = drupal_map_assoc(array(
			'1' => 'Sample Workshop',
			'2' => 'Sample Other Workshop'), 'workshop_id');
	$form['workshop']['workshop_id'] = array(
		'#type' => 'checkboxes',
		'#title' => t('Select the desired workshop(s) from the following list'),
		'#default_value' => variable_get('workshop_id', ''),
		'#options' => $workshops,
	);
      $form['workshop']['presentation_times'] = array(
              '#type' => 'textarea',
              '#title' => t('Enter the desired delivery dates and times. See the above links for the workshop duration. Please suggest two or three acceptable dates and times, as we may be booked on your first choice. A minimum of <b>two weeks notice</b> is requested'),
              '#default_value' => variable_get('presentation_times', ''),
              '#rows' => 3
      );
	$form['workshop']['additional_details'] = array(
		'#type' => 'textarea',
		'#title' => t('Please share any other information you would like us to know, or ask any questions you have for us'),
		'#default_value' => variable_get('additional_details', ''),
		'#rows' => 4
	);

	// Contact Information
	$form['contact'] = array(
		'#type' => 'fieldset',
		'#title' => t('Contact Information'),
		'#collapsible' => FALSE,
		'#collapsed' => FALSE,
	);
	$form['contact']['name'] = array(
		'#type' => 'textfield',
		'#title' => t('Name'),
		'#size' => 30,
		'#default_value' => variable_get('name', ''),
		'#description' => t('Contact name'),
	);
	$form['contact']['grades'] = array(
		'#type' => 'textfield',
		'#title' => t('Grades'),
		'#size' => 15,
		'#default_value' => variable_get('grades', ''),
	);
	$form['contact']['class_sizes'] = array(
		'#type' => 'textfield',
		'#title' => t('Class Sizes'),
		'#size' => 15,
		'#default_value' => variable_get('class_sizes', ''),
	);
	$form['contact']['school_name'] = array(
		'#type' => 'textfield',
		'#title' => t('School'),
		'#size' => 40,
		'#default_value' => variable_get('school_name', ''),
	);
	$form['contact']['school_street_address'] = array(
		'#type' => 'textfield',
		'#title' => t('School Street Address'),
		'#size' => 50,
		'#default_value' => variable_get('school_street_address', ''),
	);
	$form['contact']['phone_number'] = array(
		'#type' => 'textfield',
		'#title' => t('Phone number'),
		'#size' => 12,
		'#default_value' => variable_get('phone_number', ''),
	);
	$form['contact']['email_address'] = array(
		'#type' => 'textfield',
		'#title' => t('Email Address'),
		'#size' => 50,
		'#default_value' => variable_get('email_address', ''),
	);

	$form['submit'] = array('#type' => 'submit', '#value' => t('Book Workshop'));
	return $form;
}

function workshopbooking_page()
{
	return drupal_get_form('workshopbooking_form');
}

function workshopbooking_form_validate($form, &$form_state)
{
	if ($form_state['values']['presentation_times'] == '')
	{
		form_set_error('', t('You must provide the times at which you wish to receive the presentation'));
	}
	if ($form_state['values']['name'] == '')
	{
		form_set_error('', t('You must provide your name as the contact.'));
	}
	if ($form_state['values']['grades'] == '')
	{
		form_set_error('', t('You must select the grades to receive the presentation.'));
	}
	if ($form_state['values']['class_sizes'] == '')
	{
		form_set_error('', t('You must provide the size of the class receiving the presentation.'));
	}
	if ($form_state['values']['school_name'] == '')
	{
		form_set_error('', t('You must provide the name of the school at which the the presentation will take place.'));
	}
	if ($form_state['values']['school_street_address'] == '')
	{
		form_set_error('', t('You must provide the address of the school at which the presentation will take place.'));
	}
	if ($form_state['values']['phone_number'] == '')
	{
		form_set_error('', t('You must provide a contact phone number.'));
	}
	if (!preg_match('/^([0-9]){3}-?([0-9]){3}-?([0-9]){4}$/',$form_state['values']['phone_number']))
	{
		form_set_error('', t('The phone number must be in the form 613-321-1234 or 6133211234'));
	}
	if ($form_state['values']['email_address'] == '')
	{
		form_set_error('', t('You must provide a contact email address.'));
	}
	if (!preg_match('/([a-zA-Z0-9_\.]){3,20}@([a-zA-Z0-9_\.]){3,20}.([a-zA-Z0-9_\.]){2,5}/',$form_state['values']['email_address']))
	{
		form_set_error('', t('The email address provided is not formatted correctly.'));
	}
}

function workshopbooking_form_submit($form, &$form_state)
{
	die("<pre>The submit got called");
	drupal_set_message(t('Your request has been submitted.'));
}

print workshopbooking_page();
?>
ludo1960’s picture

Fatal error: Cannot use string offset as an array in /var/www/includes/form.inc on line 976

lonerzzz’s picture

Thanks for trying out the code. I don't get the error you are seeing, but do see the error that I posted at the top of the message:

Invalid argument supplied for foreach() in /var/www/drupal/includes/form.inc on line 1197.

Otherwise the code shows the form for me, but does not hit validate or submit when I hit the submit button. It also doesn't show the checkboxes, but that is another issue.