Skip to content

Commit

Permalink
Merge pull request #18 from kburdon/master
Browse files Browse the repository at this point in the history
Properly validate true on multiple countries (fixes #18)
  • Loading branch information
Propaganistas committed Dec 8, 2015
2 parents a5cb0c7 + df1ff2e commit 3d23f6f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/PhoneValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,21 @@ public function validatePhone($attribute, $value, array $parameters, Validator $
return $phoneUtil->isValidNumber($phoneProto);
}

// Force validation of number against the specified country.
return $phoneUtil->isValidNumberForRegion($phoneProto, $country);
// Validate number against the specified country. Return only if success.
// If failure, continue loop to next specfied country
$success = $phoneUtil->isValidNumberForRegion($phoneProto, $country);

if ($success) {
return true;
}
}

} catch (NumberParseException $e) {
// Proceed to default validation error.
}
}

// All specified country validations have failed.
return false;
}

Expand Down
5 changes: 4 additions & 1 deletion tests/TestPhoneValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function testValidatePhoneWithDefaultCountryNoType()
// Validator with multiple country values, one correct.
$this->assertTrue($this->performValidation(['value' => '016123456', 'params' => 'BE,NL']));

// Validator with multiple country values, value correct for second country in list.
$this->assertTrue($this->performValidation(['value' => '016123456', 'params' => 'NL,BE']));

// Validator with multiple wrong country values.
$this->assertFalse($this->performValidation(['value' => '016123456', 'params' => 'DE,NL']));
}
Expand Down Expand Up @@ -165,4 +168,4 @@ public function testPhoneFormatHelperFunction()
$this->assertEquals($expected, $actual);
}

}
}

0 comments on commit 3d23f6f

Please sign in to comment.