Skip to main content
added 180 characters in body; edited title
Source Link
Dominic Woodman
  • 2.2k
  • 4
  • 36
  • 60

How to merge two forms (with separate purposes) and have one submit?

CONTEXT: The custom form creates a node, but you have to be logged in to do it. To lower the barrier I want users to be able to create the node and register on the same screen.

(Ideally the user register form won't appear if they're logged in.)

How to merge two forms and have one submit?

(Ideally the user register form won't appear if they're logged in.)

How to merge two forms (with separate purposes) and have one submit?

CONTEXT: The custom form creates a node, but you have to be logged in to do it. To lower the barrier I want users to be able to create the node and register on the same screen.

(Ideally the user register form won't appear if they're logged in.)

Tweeted twitter.com/#!/StackDrupal/status/381844625281253376
Source Link
Dominic Woodman
  • 2.2k
  • 4
  • 36
  • 60

How to merge two forms and have one submit?

Problem

I'm trying to combine two forms. A custom form and the user register form. I want the user to be able to fill in both and submit them together. How would I do this?

(Ideally the user register form won't appear if they're logged in.)

So Far

I tried calling two forms on the page but they have different submit buttons.

function postajob_page_callback() { $build = array(); $build['form_one'] = drupal_get_form('postajob_form'); $build['form_two'] = drupal_get_form('user_register_form'); return $build; } 

So I called the user register validate and submit handlers in the custom form validate and submit calls.

function postajob_form_validate($form, &$form_state) { user_register_validate($form, $form_state); ... } function postajob_form_submitform, &$form_state) { user_register_submit($form, $form_state); ... } 

This calls the function but because the form doesn't contain any of the fields (i.e. username, email, password etc.) it throws a horrible error:

Notice: Undefined index: administer_users in user_register_submit() (line 3824 of C:\XAMPP\htdocs\drupal5\modules\user\user.module). Notice: Undefined index: pass in user_register_submit() (line 3827 of C:\XAMPP\htdocs\drupal5\modules\user\user.module). Notice: Undefined index: mail in user_register_submit() (line 3838 of C:\XAMPP\htdocs\drupal5\modules\user\user.module). Notice: Undefined index: #user in user_register_submit() (line 3840 of C:\XAMPP\htdocs\drupal5\modules\user\user.module). Warning: Creating default object from empty value in entity_form_submit_build_entity() (line 8009 of C:\XAMPP\htdocs\drupal5\includes\common.inc). PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'name': INSERT INTO {users} (uid, created, data) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 165 [:db_insert_placeholder_1] => 1379868880 [:db_insert_placeholder_2] => a:6:{s:16:"ckeditor_default";s:1:"t";s:20:"ckeditor_show_toggle";s:1:"t";s:14:"ckeditor_width";s:4:"100%";s:13:"ckeditor_lang";s:2:"en";s:18:"ckeditor_auto_lang";s:1:"t";s:17:"mimemail_textonly";i:0;} ) in drupal_write_record() (line 7166 of C:\XAMPP\htdocs\drupal5\includes\common.inc). 

So I tried to merge the forms using info from [here][1] and I added the following two lines of code just before the submit button in the form.

function postajob_form($form, &$form_state) { .... $extra = user_register_form($form, $form_state); $form = array_merge($form, $extra); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; } 

This adds the form but seems to break the submit button and both submit buttons call the user_register submit not the custom form submit. Any ideas? [1]: http://www.thecarneyeffect.co.uk/merging-two-forms-together-drupal