When using files or images as a field type, and requiring that the user supply a minimum number of files or images (when the cardinality is set to unlimited), the number of files that the user uploaded is not correctly calculated.

The issue is that $total_items in field_validation_number_of_selections_validator.inc is one more than it should be.

The $total_items count is derived $form_state['values']->{$the_field}[$language]. If you do a hook_node_submit() and look in the $form_state variable you'll see Drupal adds an extra file array with an fid of 0, I have no idea why!

My first solution was to load the node with node_load, then look at the number of fields in there, until I realised that the node isn't saved at this point...

The solution for my case is to check for fid's of 0 and remove them from $form_state before the count is calculated, but this won't work for everything, I may be missing the point of this plugin, it seems to have been thought up for term references or check boxes or something.

I'll attach a patch in a sec.

CommentFileSizeAuthor
#1 field_validation-2028335.patch847 byteser_d0s
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

er_d0s’s picture

FileSize
847 bytes
kyleoliveira’s picture

Priority: Normal » Major

I am running into this problem as well. To duplicate, make a field with a checkbox widget that accepts an Unlimited number of selections. Choose the Number of selections validator. Set the minimum to 2 and make it display the count in the error message (i.e. [count]). Submit your form without checking any of the checkboxes. The error message will fire and the count will be 1, even though it should be 0.

For now, I'll probably work around the issue with the PHP validator in the deprecated validators module. This seems like something that wouldn't be hard to fix, but is very important to get working.