CakeEmail be able to setting custom email validation rule

This commit is contained in:
nojimage 2013-06-29 02:55:16 +09:00
parent 202b753c63
commit 175280ad79
2 changed files with 45 additions and 4 deletions

View file

@ -554,7 +554,7 @@ class CakeEmail {
*/
protected function _setEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email)) {
if (!Validation::email($email, false, $this->_emailRegex)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) {
@ -568,7 +568,7 @@ class CakeEmail {
if (is_int($key)) {
$key = $value;
}
if (!Validation::email($key)) {
if (!Validation::email($key, false, $this->_emailRegex)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value;
@ -608,7 +608,7 @@ class CakeEmail {
*/
protected function _addEmail($varName, $email, $name) {
if (!is_array($email)) {
if (!Validation::email($email)) {
if (!Validation::email($email, false, $this->_emailRegex)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
}
if ($name === null) {
@ -622,7 +622,7 @@ class CakeEmail {
if (is_int($key)) {
$key = $value;
}
if (!Validation::email($key)) {
if (!Validation::email($key, false, $this->_emailRegex)) {
throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
}
$list[$key] = $value;

View file

@ -286,6 +286,47 @@ class CakeEmailTest extends CakeTestCase {
$this->assertSame($regex, $email->emailRegex());
}
/**
* Tests that it is possible set custom email validation
*/
public function testCustomEmailValidation() {
$regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
$this->CakeEmail->emailRegex($regex)->to('pass.@example.com');
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->addTo('pass..old.docomo@example.com');
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->reset();
$emails = array(
'pass.@example.com',
'pass..old.docomo@example.com'
);
$additionalEmails = array(
'.extend.@example.com',
'.docomo@example.com'
);
$this->CakeEmail->emailRegex($regex)->to($emails);
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
), $this->CakeEmail->to());
$this->CakeEmail->addTo($additionalEmails);
$this->assertSame(array(
'pass.@example.com' => 'pass.@example.com',
'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
'.extend.@example.com' => '.extend.@example.com',
'.docomo@example.com' => '.docomo@example.com',
), $this->CakeEmail->to());
}
/**
* testFormatAddress method
*