-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16638 from noone-silent/T16637-allow-utf8-email-i…
…n-local-part [#16637] - refactor: allow utf8 in email local part.
- Loading branch information
Showing
3 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,16 @@ use Phalcon\Filter\Validation\AbstractValidator; | |
* ] | ||
* ) | ||
* ); | ||
* | ||
* $validator->add( | ||
* "tä[email protected]", | ||
* new EmailValidator( | ||
* [ | ||
* "message" => "The e-mail is not valid", | ||
* "allowUTF8" => true, | ||
* ] | ||
* ) | ||
* ); | ||
* ``` | ||
*/ | ||
class Email extends AbstractValidator | ||
|
@@ -58,7 +68,8 @@ class Email extends AbstractValidator | |
* @param array options = [ | ||
* 'message' => '', | ||
* 'template' => '', | ||
* 'allowEmpty' => false | ||
* 'allowEmpty' => false, | ||
* 'allowUTF8' => false, | ||
* ] | ||
*/ | ||
public function __construct(array! options = []) | ||
|
@@ -71,12 +82,19 @@ class Email extends AbstractValidator | |
*/ | ||
public function validate(<Validation> validation, var field) -> bool | ||
{ | ||
var value = validation->getValue(field); | ||
var value, flags; | ||
|
||
let value = validation->getValue(field); | ||
if this->allowEmpty(field, value) { | ||
return true; | ||
} | ||
|
||
if !filter_var(value, FILTER_VALIDATE_EMAIL) { | ||
let flags = FILTER_DEFAULT; | ||
if (this->getOption("allowUTF8")) { | ||
let flags = FILTER_FLAG_EMAIL_UNICODE; | ||
} | ||
|
||
if !filter_var(value, FILTER_VALIDATE_EMAIL, flags) { | ||
validation->appendMessage( | ||
this->messageFactory(validation, field) | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon Framework. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Tests\Unit\Filter\Validation\Validator; | ||
|
||
use Phalcon\Filter\Validation; | ||
use UnitTester; | ||
|
||
class EmailCest | ||
{ | ||
/** | ||
* Tests Filter\Validation\Validator\Email :: validate | ||
* | ||
* @param UnitTester $I | ||
* | ||
* @author n[oO]ne <[email protected]> | ||
* @since 2024-08-19 | ||
*/ | ||
public function validEmail(UnitTester $I) | ||
{ | ||
$I->wantToTest('Filter\Validation\Validator\Email - valid email'); | ||
|
||
$validation = new Validation(); | ||
$validation->add('email', new Validation\Validator\Email()); | ||
$I->assertEmpty($validation->validate(['email' => '[email protected]'])); | ||
} | ||
|
||
/** | ||
* Tests Filter\Validation\Validator\Email :: validate | ||
* | ||
* @param UnitTester $I | ||
* | ||
* @author n[oO]ne <[email protected]> | ||
* @since 2024-08-19 | ||
*/ | ||
public function invalidEmail(UnitTester $I) | ||
{ | ||
$I->wantToTest('Filter\Validation\Validator\Email - invalid email'); | ||
|
||
$validation = new Validation(); | ||
$validation->add('email', new Validation\Validator\Email()); | ||
$I->assertNotEmpty($validation->validate(['email' => '[email protected]'])); | ||
} | ||
|
||
/** | ||
* Tests Filter\Validation\Validator\Email :: validate | ||
* | ||
* @param UnitTester $I | ||
* | ||
* @author n[oO]ne <[email protected]> | ||
* @since 2024-08-19 | ||
*/ | ||
public function emailWithoutUTF8Success(UnitTester $I) | ||
{ | ||
$I->wantToTest('Filter\Validation\Validator\Email - without utf8 is ok'); | ||
|
||
$validation = new Validation(); | ||
$validation->add('email', new Validation\Validator\Email()); | ||
$I->assertEmpty($validation->validate(['email' => '[email protected]'])); | ||
} | ||
|
||
/** | ||
* Tests Filter\Validation\Validator\Email :: validate | ||
* | ||
* @param UnitTester $I | ||
* | ||
* @author n[oO]ne <[email protected]> | ||
* @since 2024-08-19 | ||
*/ | ||
public function emailAllowEmptyFails(UnitTester $I) | ||
{ | ||
$I->wantToTest('Filter\Validation\Validator\Email - empty is not ok'); | ||
|
||
$validation = new Validation(); | ||
$validation->add('email', new Validation\Validator\Email()); | ||
$I->assertNotEmpty($validation->validate(['email' => ''])); | ||
} | ||
|
||
/** | ||
* Tests Filter\Validation\Validator\Email :: validate | ||
* | ||
* @param UnitTester $I | ||
* | ||
* @author n[oO]ne <[email protected]> | ||
* @since 2024-08-19 | ||
*/ | ||
public function emailAllowEmptySuccess(UnitTester $I) | ||
{ | ||
$I->wantToTest('Filter\Validation\Validator\Email - empty is ok'); | ||
|
||
$validation = new Validation(); | ||
$validation->add('email', new Validation\Validator\Email(['allowEmpty' => true])); | ||
$I->assertEmpty($validation->validate(['email' => ''])); | ||
} | ||
|
||
/** | ||
* Tests Filter\Validation\Validator\Email :: validate | ||
* | ||
* @param UnitTester $I | ||
* | ||
* @author n[oO]ne <[email protected]> | ||
* @since 2024-08-19 | ||
*/ | ||
public function emailWithUTF8Fail(UnitTester $I) | ||
{ | ||
$I->wantToTest('Filter\Validation\Validator\Email - with utf8 fails'); | ||
|
||
$validation = new Validation(); | ||
$validation->add('email', new Validation\Validator\Email()); | ||
$I->assertNotEmpty($validation->validate(['email' => 'tä[email protected]'])); | ||
} | ||
|
||
/** | ||
* Tests Filter\Validation\Validator\Email :: validate | ||
* | ||
* @param UnitTester $I | ||
* | ||
* @author n[oO]ne <[email protected]> | ||
* @since 2024-08-19 | ||
*/ | ||
public function emailWithUTF8Success(UnitTester $I) | ||
{ | ||
$I->wantToTest('Filter\Validation\Validator\Email - with utf8 success'); | ||
|
||
$validation = new Validation(); | ||
$validation->add('email', new Validation\Validator\Email(['allowUTF8' => true])); | ||
$I->assertEmpty($validation->validate(['email' => 'tä[email protected]'])); | ||
} | ||
} |