If a regex (or any) rule is set on a field that is not required to be filled out in the webforms node, "empty" should be a valid state.

When I apply a regex to make sure a text field is all alphanumeric and spaces, having nothing in the field sets off the regex rule.

Comments

gregarios’s picture

Title: Validator Rules On Non-Required Fields » Validator Rules Should All Validate On Empty Non-Required Fields
gregarios’s picture

Status: Active » Patch (to be ported)
StatusFileSize
new1.73 KB

Attached is a patch to correct the behavior in the webform_validation.validators.inc file. The patch allows these built-in validators to ignore fields with no content in them:

  • Numeric values
  • Minimum length
  • Maximum length
  • Regular expression

Validating the presence of content in a field can be left to the Webforms module itself... so it is not needed in this module anyway.

This patch allows such things as a non-required Zip Code field to not have any value and still validate -- while the same field, if filled, will have to pass the validator's requirements.

gregarios’s picture

StatusFileSize
new2.53 KB

The attached is a new patch which includes a new Email Validator option in the list of default validator rules. Please apply in a new release if you would. It includes the previous patch enhancements from comment #2 above as well. Use on version 6.x-1.0.

gregarios’s picture

Title: Validator Rules Should All Validate On Empty Non-Required Fields » Validator Rules Should All Validate On Empty Fields

Title change... "fields required" is handled by the core Webforms module. There is no need to verify that the field is not empty, only that the data in it validates, if it exists.

svendecabooter’s picture

That makes sense gregarios.
I applied the code in #2 to the dev release.

With regards to the email validation function: I'm not sure if it's really needed.
If you add an 'email' form element to a webform, the webform core module already does validation (function _webform_validate_email in email.inc).
However this function uses the default Drupal valid_email_address(). I'm not sure how that compares to filter_var($val, FILTER_VALIDATE_EMAIL), and whether one or the other is better, so i'm putting this on hold for now.
If your suggested function would be a better way to validate email addresses, perhaps it would make more sense to try to get that implemented upstream (i.e. in the webform module or Drupal core).

svendecabooter’s picture

Assigned: Unassigned » svendecabooter
Status: Patch (to be ported) » Fixed
gregarios’s picture

With regards to the email validation function: I'm not sure if it's really needed.

I can understand that... I found that the Drupal built-in email validator didn't validate as well in testing. The filter_var($val, FILTER_VALIDATE_EMAIL) seems more strict. Can it hurt? :-) (I've already begun using the patched module on my site, too. lol)

The extra email validation helps when you want to place an email address in a 'hidden' field using PHP to gather it.

See this issue about the Core vs the filter_var: http://drupal.org/node/308138
They are talking about the Dev 8.x version of Drupal getting it... so they probably aren't interested in putting it into v6 for us. Yours would make it a choice for version 6 users.

Getting it into your module would be easier than getting it into core I believe. :-)

Thanks for getting the #2 patch in. Will you be making a version 1.1 release from the dev soon?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.