Mutli part form using the step method. Two steps. First is a simple radio button select. Second is a 3 text field select.
If invalid entries are put in the second form as determined by my validate function, it returns to the form correctly, flagging the invalid entries.
Correct the invalid entries and submit the form again, and it will return to the step 1 without submitting the form and without flagging invalid entries.
using $form_id and $form_values as input vars instead of the below results in sql errors.
using $param1, $param2, $form_values = NULL results in the same behavior.
<?php
function match_report($form_id,$param1,$form_values = NULL) {
if (! isset($form_values) ) {
$step = 1;
} else {
$step = 2;
}
$form['step'] = array(
'#type' => 'hidden',
'#value' => $step,
);
switch ($step) {
case 1:
$node = node_load(arg(2));
if ($node->type == 'group') {
$gid = $node->nid;
$matches = array();
$result = db_query(db_rewrite_sql('SELECT n.nid, n.title, e.event_start FROM {node} n INNER JOIN {matches} m ON n.nid = m.nid INNER JOIN {event} e ON m.rid = e.nid WHERE (m.gid1 = %d OR m.gid2 = %d) AND m.match_status = 0 ORDER BY e.event_start'),$gid,$gid);
if (db_num_rows($result) > 0) {
while ( $match = db_fetch_object($result) ) {
if (! $first_match) { $first_match = $match->nid; }
#$matches[$match->nid] = l(t($match->title . ' on ' . format_date($match->event_start)),'node/' . $match->nid, array('title' => t($match->title), 'target' => '_blank' ) );
$matches[$match->nid] = t($match->title . ' on ' . format_date($match->event_start));
}
} else {
$form['body'] = array();
form_set_error('body','No scheduled matches for your group. Perhaps you have not opted to participate in any leagues.');
return $form;
}
$form['mid'] = array(
'#type' => 'radios',
'#title' => t($league->title),
'#default_value' => $first_match,
'#options' => $matches,
'#description' => t('Select the match to report your results for and click Submit or click on the match for a description.')
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Select Match'));
$form['gid'] = array('#type' => 'hidden', '#value' => $gid);
$form['#multistep'] = TRUE;
$form['#redirect'] = FALSE;
return $form;
} else {
$form['body'] = array();
form_set_error('body','Group specified is not valid.');
return $form;
}
break;
case 2:
$mid = $form_values['mid'];
$gid = $form_values['gid'];
$match_node = node_load($mid);
$gid1_title = get_group_title($match_node->gid1);
$gid2_title = get_group_title($match_node->gid2);
$form['scores'] = array(
'#type' => 'fieldset',
'#title' => t('Match ' . $match_node->title),
'#description' => t('Entering final match results.'),
'#tree' => FALSE,
);
$form['scores']['gid1_score'] = array(
'#type' => 'textfield',
'#title' => t($gid1_title . ' Total Score'),
'#required' => TRUE,
'#default_value' => $match_node->gid1_score,
'#description' => t('Enter total score for all rounds for ' . $gid1_title . '')
);
$form['scores']['gid2_score'] = array(
'#type' => 'textfield',
'#title' => t($gid2_title . ' Total Score'),
'#required' => TRUE,
'#default_value' => $match_node->gid2_score,
'#description' => t('Enter total score for all rounds for ' . $gid2_title . '')
);
/*$form['scores']['match_winner'] = array(
'#type' => 'radios',
'#title' => t('Match Winner'),
'#default_value' => 0,
'#options' => array(1=> $gid1_title, 2=> $gid2_title, 0=> 'Tie'),
'#description' => t('Select the match winner.')
);
*/
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => t('Match Results Overview'),
'#default_value' => $match_node->body,
'#rows' => 10,
'#required' => TRUE,
'#weight' => -1,
'#description' => t('Briefly describe how the match went.')
);
$form['body_filter']['format'] = filter_form($node->format);
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit Results'));
$form['gid'] = array('#type' => 'hidden', '#value' => $gid);
$form['mid'] = array('#type' => 'hidden', '#value' => $mid);
$form['#multistep'] = TRUE;
$form['#redirect'] = 'node/' . $match_node->nid . '/images';
return $form;
break;
case 3:
}
}
function get_group_title($gid) {
$group_node = node_load($gid);
return $group_node->title;
}
function match_report_submit($form_id, $form_values) {
$final_step = 2;
if ($form_values['step'] == $final_step) {
$node = node_load($form_values['mid']);
$node->match_status = 1;
$node->gid1_score = trim($form_values['gid1_score']);
$node->gid2_score = trim($form_values['gid2_score']);
$node->body = $form_values['body'];
if ($form_values['gid1_score'] > $form_values['gid2_score']) {
$node->match_winner = $form_values['gid1'];
} elseif ($form_values['gid2_score'] > $form_values['gid1_score']) {
$node->match_winner = $form_values['gid2'];
} else {
$node->match_winner = 0;
}
node_save($node);
}
}
function match_report_validate($form_id, $form_values) {
$final_step = 2;
if ($form_values['step'] == $final_step) {
if (! preg_match("/^\d+$/",$form_values['gid1_score'],$matches) ) {
form_set_error('gid1_score',t('Please enter a valid score. Only numbers are allowed.') );
}
if (! preg_match("/^\d+$/",$form_values['gid2_score'],$matches) ) {
form_set_error('gid2_score',t('Please enter a valid score. Only numbers are allowed.') );
}
}
}
Comments
Comment #1
Daren Schwenke commentedLets try to get the formatting to stick this time:
Comment #2
walkah commentedComment #3
coofercat commentedThis looks the same as a thread I started (sorry, I searched, but I didn't find this one at the time!).
FAPI Loads Wrong Form When Re-Submitting Multi-step Form"
Comment #4
Berto commentedIn Drupal 5.x, the best workaround I can think of is this:
function _whatever_form_validate($form_id, $form_values) {
$current_step_to_check = $form_values['step'] -1;
if($current_step_to_check == 1) {
form_set_error("ERROR MESSAGE HERE - WONT SHOW UP");
drupal_set_message("ERROR MESSAGE HERE - WILL SHOW UP");
} else {
// Check the rest or whatever...
}
}
form_set_error will make sure that you don't even MAKE it to the second page. However, drupal_set_message will come through.
The only problem with this is that it's not in red. Oh well.
Comment #5
dpearcefl commentedDue to the age of the last comment on this issue and due to the fact that D5 is no longer supported, I am closing this issue.