As it stands the sum validator only works on numeric fields. A site I was recently working on wanted to have a sum validation mechanic but with much more restrictive input options than a mostly freeform number field. Specifically they wanted a select/dropdown pre-filled with the allowed values.

The changes to support this are trivial and I thought it might be potentially useful to others. I'm attaching a patch against webforms_validation.validators.inc from 7.x-1.10 that adds sum validator support for the select component type.

In order for this to work the select component values must be set to something that PHP's is_numeric() will recognize as a number. If any selected field contains something other than a number it will be ignored (interpreted as zero) when computing the sum.

I'd love to see this become an officially supported option, as I think it is a pretty straightforward and useful behavior to be able to sum numbers supplied via a dropdown vs only freeform entry. It enables better UX for forms by preventing people from entering data that will fail validation anyways. I'd be happy to discuss alternate solutions if someone has a better way of solving this problem.

CommentFileSizeAuthor
webform_validation.validators.inc_.patch592 byteszaphoyd
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

zaphoyd’s picture

Title: Allow select fields to be targeted by "sum" validator » Allow select fields to be targeted by sum validator
Liam Morland’s picture

Status: Active » Needs work

Thanks for the patch. It is not working with the "multiple" option.

Please create your patch with "git diff" (see instructions). The patch as it was did not apply with "git apply". When the issue has a patch, set it to "needs review".

zaphoyd’s picture

Ah, that is a good point. I'll see what I can do.