Community & Support

Drupal Studs help me with my form_alter hook ( I am almost there)

So I think I am almost there conceptually but need some missing pointers.

Objective is to add a few more fields to the normal user registration form, style it a little, then submit it with storing the extra fields in a table.

This is what I have so far. Can someone give me the final nudge and get me going. Please help me. Also how do I apply some minor styling like aligning the new form fields ?

Thank you so much !!!!!!!!!

function module_menu() {
$items = array();
$items['school/registration'] = array(
      'title' => 'Upgraded Registration Form',
      'page callback'    =>'module_school_register',
      'type' => MENU_CALLBACK           
    );

return $items;
}//end of the function



function module_school_register(){
return drupal_get_form('form_school_register');
}//end of the function


function module_school_form_alter(&$form, $form_state, $form_id)
{

dsm($form_id);

   if ($form_id == 'user_registration_form')
   {
// modify the "#submit" form property by prepending another submit handler array
$form['#submit'] = array_merge(
      array('_module_registration_submit' => array()),
      $form['#submit']
    );

   }
}


function _module_registration_submit($form_id, $form_values) {
  // store extra data in different table
}


function module_registration_validate($form, &$form_state)
{
$error=0;
   //Validation stuff here, set $error to true if something went wrong, or however u want to do this.  Completely up to u in how u set errors.
   if ($error)
   {
      form_set_error('new_field_name', 'AHH SOMETHING WRONG!');
   }
}

Comments

People could help you much

People could help you much better if you made your question much more specific....what was not working...or what is working...

As far as aligning fields, CSS would be the way to handle that.

The site doesn't even let me

The site doesn't even let me go to "school/registration"

It says view removed or something. Basically I just want you to take a look at my code and tell me if it looks OK

Where is this

Where is this module_school_register function? Does it return $form? In the original drupal_get_form callback which I've commented out you've called it form_school_register...!

Pobster

OK I will try my best to

OK I will try my best to explain so please bare with me

There is a working user registration form called: form123

I wish to add a few elements to this form and save these extra fields database.

<?php

/**
* Implementation of hook_menu().
*/

function modulename_menu() {
$items = array();
$items['school/registration'] = array(
      'title' => 'some registration',
      'page callback'    =>'modulename_school_register',
      'type' => MENU_CALLBACK           
    );

return $items;
}//end of the function



function modulename_school_register(){

return drupal_get_form('form123');

}//end of the function


function modulename_school_form_alter(&$form, $form_state, $form_id)
{

dsm($form_id);

   if ($form_id == 'user_registration_form')
   {
// modify the "#submit" form property by prepending another submit handler arra
$form['#submit'] = array_merge(
      array('_modulename_registration_submit' => array()),
      $form['#submit']
    );

   }
}



function _modulename_registration_submit($form_id, $form_values) {
  // save extra fields in database
}


function modulename_school_register_validate($form, &$form_state)
{
$error=0;
   //Validation stuff here, set $error to true if something went wrong, or however u want to do this.  Completely up to u in how u set errors.
   if ($error)
   {
      form_set_error('new_field_name', 'AHH SOMETHING WRONG!');
   }
}

It wont even find the page

It wont even find the page "sitename.com/school/registration"

says Skipping broken view

<?phpfunction

<?php
function module_school_menu() {  // What actually *is* the name of your module?  Your hooks differ one is module one is module_school
$items = array();
$items['school/registration'] = array(
     
'title' => 'Upgraded Registration Form',
     
'page callback'    =>'drupal_get_form', // See below
     
'page arguments'    => array('module_school_register'),
     
'type' => MENU_CALLBACK           
   
);

return
$items;
}
//end of the function


// This isn't necessary if this is all it does
//function module_school_register(){
//return drupal_get_form('form_school_register');   
//}//end of the function


function module_school_form_alter(&$form, &$form_state, $form_id) // See your module name differs to the above
{

dsm($form_id);

   if (
$form_id == 'user_registration_form')
   {
// modify the "#submit" form property by prepending another submit handler array
$form['#submit'][] = '_module_registration_submit'; // What was all that about then?
$form['#validate'][] = 'module_registration_validate'; // This needs to be specified
  
}
}


function
_module_registration_submit($form_id, &$form_state) {
 
// store extra data in different table
}


function
module_registration_validate(&$form, &$form_state)
{
$error=0;
  
//Validation stuff here, set $error to true if something went wrong, or however u want to do this.  Completely up to u in how u set errors.
  
if ($error)
   {
     
form_set_error('new_field_name', 'AHH SOMETHING WRONG!');
   }
}
?>

Notes:
You've set up a menu callback for a form you don't have in this code. I assume you've just not provided it, but it's pretty evident from your coding that guesswork is playing quite a large role ;o) You really MUST provide a hook_perm and add "access arguments" into your menu item.
Whilst you can use form_alter to do the above, Drupal provides hook_user to maintain user registration additions. It's pointless me rewriting your code to show this as it's very different, perhaps take a look at; http://drupalcode.org/viewvc/drupal/contributions/modules/gamertags/game... or at the very least the API page; http://api.drupal.org/api/function/hook_user