Hi there,

I recently discovered the webform module and must say it's an amazing piece of work.

I have to develop some forms which allow members of my table tennis club to register for tournaments. For each tournament, they're allowed to register for a maximum of 2 series.

Is there any way to add additional field validation to make sure a member can only make 2 selections from a group of checkboxes?

Thanks,
Joris

Comments

quicksketch’s picture

Read over the Webform Handbook (http://drupal.org/handbook/modules/webform) where there are several examples of using the Additional Validation field to add custom validation to the form. This particular situation isn't listed, but it's pretty similar to some of the other validations.

osieke’s picture

Thanks for your reply...I've been trying to get this working, but I seem to be failing somewhere. I have a form with a select field with 4 options (checkboxes). The form should limit the user to a maximum of 2 selections. I added the following code to the additional validation:

$checkboxcount = 0;
$reeksen=$form_values['submitted_tree']['reeks']; 
foreach( $reeksen as $value )
{ 
	$checkboxcount = $checkboxcount + 1;
}
if( $checkboxcount > 2 )
{
form_set_error('reeks', 'You can only select 2 series.');
}

Now, the form always displays the error even if you only select 2 or 1. It seems to count all available checkboxes instead of the ones that are checked.

Any idea what the code above is missing to only count checked boxes?

Thanks,

osieke’s picture

Found it!

Had to add: if($value) { $checkboxcount = $checkboxcount + 1; }

manogolf’s picture

I dropped the code above including the "Found it!" into the additional validation field and received the following error:

warning: Invalid argument supplied for foreach() in /var/www/drupalroot/drupal6/sites/all/modules/webform/webform.module(1657) : eval()'d code on line 5.

Bear in mind I have little knowledge of coding with php (or any language for that matter) and do not understand the reeks references.

What I am attempting too accomplish is using a select type webform with 136 choices but not allow more than 5 radio buttons selected by user. Less than 5 is also undesireable. The user should see an error if exactly five is not selected.

I would also like the see the list in three columns instead of one very long list. In fact there is a whole list of things I would like the form too accomplish which I will not be able to perform with my limited abilities. Can you recommend a for hire professional?

manogolf’s picture

Can someone let me know if I'm getting close?

<?php
// Grab variables.
$allplayers = $form_values['submitted_tree']['allplayers'];
 
// Check for values.
if ($select == ">5" ) 
{
  form_set_error('submitted][select', t('Only Five Selections Are Allowed. Please Revise'));
  
}
?>
manogolf’s picture

Frustrated that I could not edit the example validation snippets and make them work for my webform I returned to the osieke example and finally found success. I should have not wandered away from this code as it does function as needed.

So with only a few changes here is what I used to not allow more than five selections made from those available.

<?php
$checkboxcount = 0;
$Field_Keyen=$form_values['submitted_tree']['Field_Key'];
foreach( $Field_Keyen as $value )
{
    if($value) { $checkboxcount = $checkboxcount + 1; }
}
if( $checkboxcount > 5 )
{
form_set_error('Field_Key', 'You are allowed only 5 selections. Please revise and resubmit');
}
?>

Where Field_Key equals the value shown in advanced settings on the form components. In my case all instances of Field_Key were substituted with allplayers; the name. I don't know if en is needed after Field_Key in the above but it worked so I left it.

I also wanted an error if <5 selections were made. The easy solution was copy the code then paste again below the first instance. The only changes were < 5 instead of > 5 and the error message itself.

There is a way to code a value range which avoids the copy/paste step but I couldn't figure out how.

quicksketch’s picture

Status: Active » Closed (fixed)

Closing, sounds like this is solved for the large part. Help with custom coding is no longer provided in the Webform issue queue.