When using the E.123 option for validating free-form international numbers, I ran into a few problems with the phone.int.inc file, which are addressed in the patch.

This patch fixes, "is not a valid Italian phone number".

  • The default error message mentioned Italy, which is clearly incorrect.
  • The Regex for finding the country code needed the code to be separated from the rest of the number, generally with a space, or it couldn't be found. The attached patch goes through the known country codes until it finds one (starting with the longest, first), which is essentially how the PSTN does it.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Greg Boggs’s picture

Issue summary: View changes

Added error message to description.

MiroslavBanov’s picture

Patch did not apply to current 7.x-1.x, rerolled the patch with a small change:
< - drupal_set_message('langue cc = ' . $cc, 'error');
> - //drupal_set_message('langue cc = ' . $cc, 'error');

Note that I do not want to take credit for this contribution.

ingomueller.net’s picture

I just ran into the same problem. Thanks a lot for the patch, it seems to work fine for me now! Without this patch, this module seems hardly usable (with international numbers), since it is very unobvious for a user that the space is needed and the error message leads into a totally wrong direction. It'd be great if the patch could be integrated.

extect’s picture

Status: Needs review » Reviewed & tested by the community

Patch is working absolute fine for me as well.
Setting this to RTBC so that it'll hopefully be committed soon.

bbinkovitz’s picture

Previous patch didn't apply for me, and introduced weird whitespace, so I re-rolled it.

bbinkovitz’s picture

bradjones1’s picture

FYI, the patch rolled in #1 currently applies to -dev; the original patch and the one in #5 do not. This is still RTBC.

roderik’s picture

I'm tempted to set this back to Needs Review, because of #2.

Notes and discoveries:

#1
the huge regex isn't necessary. I'm pretty sure a simpler solution using substr will save memory (and possibly CPU).

#2
the array in phone_country_code_convert() has duplicate keys, so that needs fixing. I placed 'us' before 'ca' so 'us' is returned for input value 1, because that is also the behavior from before the patch. 'au' is now returned for 61 and 'it' for 39 (instead of 'cc' and 'va' respectively) because that just seems sensible.

Note that means that phone.ca.inc is never used. But that seems to be the case already!?
That's why I didn't fix this (this can be done by someone else if they want to) - I only provided clear comments.

#3
the patch makes numbers actually be validated. Before the patch, TRUE is almost always returned, because the module_load_include() is wrong, so $valid_phone_function is not called. (Unless it was loaded already.)

#4
When we're starting to validate things anyway... I changed the 'else' case to not only return TRUE, but check (using format_int_phone_number()) whether an international dialing code is present in the area code, and return FALSE if not.
(If this check isn't done, then format_int_phone_number() will still delete figures without warning you, leading to bugs like #1563692: format_int_phone_number() fails to use the appropriate country formatter and strips bits out of valid Swedish phone numbers.)

roderik’s picture

Status: Reviewed & tested by the community » Needs review

.

yoruvo’s picture

EDIT:

Scratch that, I was testing on the wrong system. The patch seems to work; I no longer get an Italian-specific error message when putting in random garbage. I haven't tested further, but I can confirm that part works fine, now.

However the string is missing a period at the end of the sentence.

gumdal’s picture

I am using 7.x-1.0-beta1 version of Phone module, can I use the patch against this beta version?

Also, which patch to use? Confused between #1, #5 and #7?

Thanks.

bradjones1’s picture

@gumdal - Generally you want to apply patches you find in the issue queues to the -dev version. So in this case you could do :

$ drush dl phone-7.x-1.x-dev
... cd to the phone module directory ...
$ wget https://www.drupal.org/files/issues/phone-international-2108707-7.patch
$ patch -p1 < https://www.drupal.org/files/issues/phone-international-2108707-7.patch
joelpittet’s picture

Status: Needs review » Needs work
+++ b/include/phone.int.inc
@@ -37,33 +37,40 @@ function valid_int_phone_number($phonenumber) {
+  // Check for correct country code.
+  // TODO: Fix problems with shared country codes. For instance, $countrycode is
+  //       never 'ca' (but always 'us'). One of the following things needs done:
+  //       - all countries sharing the same area need to be validated in the
+  //         same .inc file (e.g. phone.1.inc or phone.61.inc)
+  //       - there will need to be a separate 'pre-validate' function that
+  //         determines which country a phone number belongs to.
+  $codes = array_flip(array_unique(phone_country_codes()));
+  foreach (array(1, 2, 3) as $length) {
+    $nr_start = substr($phonenumber, 1, $length);
+    if (isset($codes[$nr_start])) {
+      $countrycode = $codes[$nr_start];
+      break;
+    }
+  }

How about not using Country Codes? US and Canada are using north american standards, should there be one file for them both. NA/North America.

HydroZ’s picture

Patch #7 did fix the wrong validation error.

DamienMcKenna’s picture

Status: Needs work » Reviewed & tested by the community

I think this approach is fine, it's better to work from something tangible as country codes that might come up in other scenarios.

lambic’s picture

I made a small modification to patch 7 so that in the case where we don't have a specific validation function for a given country then we at least check that the number is more than 5 digits. Patch attached.

lambic’s picture

Fixed patch with the correct variable name

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Needs review
joelpittet’s picture

@lambic this was RTBC already, could you provide an interdiff with your change so we can review the change, hopefully worth the status change?

See https://www.drupal.org/documentation/git/interdiff

lambic’s picture

joelpittet’s picture

Thanks for the interdiff. Why 5 digits? Isn't 911 or 411 valid north american phone numbers?

lambic’s picture

This is in international validation, so only happens if there's no country specific validation. In theory for countries with a single digit country code and no country specific validation, 3 digit phone numbers would fail, we could reduce the check to 4 digits to cover that use case, or make the number of digits settable via a variable.

shenzhuxi’s picture

All the number without country code will call valid_us_phone_number and include/phone.us.inc file doesn't exist.