[support] Validation function name when using hook_forms()

Steve Edwards killshot91 at comcast.net
Mon Nov 26 05:19:58 UTC 2007


I'm trying to use hook_forms() to use one form builder function to generate multiple forms.  In the process of writing a very long 
e-mail, I got most of it figured out, but that leads to other questions. :-)  My module will create multiple forms on one page, 
one for each project listed in the project_projects table.  Here is the code I have so far:

<?php
//$Id$

/*
  * Adds the capability to assign access to projects
  * to specific users
  */

/*
  * Implementation of hook_menu()
  */
function project_permissions_menu($may_cache) {
   $items = array();
     if ($may_cache) {
       $items[] = array(
         'path' => 'admin/project/project_permissions',
         'title' => t('Project Permissions'),
         'description' => t('Set access to projects'),
         'access' => user_access('administer projects'),
         'callback' => 'project_permissions_page',
         'type' => MENU_NORMAL_ITEM
       );
     }
     return $items;
}

function project_permissions_page() {
   $output = t('Use this page to assign permissions to each project');

   //get list of projects
   $sql = "SELECT nid, uri FROM {project_projects}";
   $result = db_query($sql);

   //for each project, create a fieldset with project and user fields
   while ($projects = mysql_fetch_object($result)) {
     $project_name = $projects->uri;
     $project_nid = $projects->nid;

     //call form function
     $output .= drupal_get_form($project_name.'_permissions_form');
   }
   return $output;
}

/*
  * Implementation of hook_forms()
  */
function project_permissions_forms() {
   $sql = "SELECT nid, uri FROM project_projects";
   $result = db_query($sql);
   while ($project = mysql_fetch_object($result)) {
     $forms[$project->uri.'_permissions_form'] = array(
     'callback' => 'project_permissions_form',
     'callback arguments' => array($project->nid, $project->uri)
     );
   }
   return $forms;
}

/*
  * Define the project permissions settings form
  */
function project_permissions_form($project_nid, $project_name) {
   $form[$project_nid] = array(
     '#type' => 'fieldset',
     '#title' => $project_name,
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
   );

   $form[$project_nid]['projects'] = array(
     '#type' => 'textfield',
     '#title' => t('Project name'),
     '#maxlength' => 30,
     '#size' => 30,
     '#default_value' => $project_name,
   );

   $form[$project_nid]['users'] = array(
     '#type' => 'select',
     '#title' => t('Users with access to project'),
     '#description' => t('Select users to be asssigned to the project'),
     '#options' => _get_user_list(),
     '#multiple' => TRUE
   );

   $form[$project_nid]['nid'] = array(
     '#type' => 'hidden',
     '#value'=> $project_nid,
   );

   $form[$project_nid]['submit'] = array(
     '#type' => 'submit',
     '#value' => 'Submit'
   );
   return $form;
}

/*
  * Gets lists of projects from project_projects table
  */
function _get_projects_list() {
   $sql = "SELECT nid, uri FROM {project_projects}";
   $result = db_query($sql) or die('Error'. mysql_error());
   while($projects = db_fetch_object($result)) {
     $options[$projects->nid] = $projects->uri;
   }
   return $options;
}

/*
  * Gets lists of users from users table
  */
function _get_user_list() {
   $sql = "SELECT uid, name FROM {users} WHERE uid > %d";
   $result = db_query($sql,0) or die('Error'. mysql_error());
   while($users = db_fetch_object($result)) {
     $options[$users->uid] = $users->name;
   }
   return $options;
}

This gets me what I want: one form for each project in a collapsible fieldset.  Now that I have a separate form for each, what do 
I name my validation function?  Is it the name of my form builder function plus '_submit'?  That seems logical, but it isn't 
working.  If I name my submit function with the name of a specific project (i.e. XYZ_permissions_form_submit() for project XYZ), 
it works like a charm, but if I just use project_permissions_form_submit(), it doesn't do anything.

Thanks.

Steve


More information about the support mailing list