Skip to content

Make email validation only validate @ sign presence, fixes #4547 #9087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 3 additions & 85 deletions app/code/Magento/Customer/Model/Metadata/Form/AbstractData.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,93 +324,11 @@ protected function _validateInputRule($value)
break;
case 'email':
/**
__("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded")
__("Invalid type given. String expected")
__("'%value%' appears to be a DNS hostname but contains a dash in an invalid position")
__("'%value%' does not match the expected structure for a DNS hostname")
__("'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'")
__("'%value%' does not appear to be a valid local network name")
__("'%value%' does not appear to be a valid URI hostname")
__("'%value%' appears to be an IP address, but IP addresses are not allowed")
__("'%value%' appears to be a local network name but local network names are not allowed")
__("'%value%' appears to be a DNS hostname but cannot extract TLD part")
__("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
__('"%1" is not a valid email address.')
*/
$validator = new \Zend_Validate_EmailAddress();
$validator->setMessage(
__('"%1" invalid type entered.', $label),
\Zend_Validate_EmailAddress::INVALID
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::INVALID_FORMAT
);
$validator->setMessage(
__('"%1" is not a valid hostname.', $label),
\Zend_Validate_EmailAddress::INVALID_HOSTNAME
);
$validator->setMessage(
__('"%1" is not a valid hostname.', $label),
\Zend_Validate_EmailAddress::INVALID_MX_RECORD
);
$validator->setMessage(
__('"%1" is not a valid hostname.', $label),
\Zend_Validate_EmailAddress::INVALID_MX_RECORD
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::DOT_ATOM
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::QUOTED_STRING
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::INVALID_LOCAL_PART
);
$validator->setMessage(
__('"%1" uses too many characters.', $label),
\Zend_Validate_EmailAddress::LENGTH_EXCEEDED
);
$validator->setMessage(
__("'%value%' looks like an IP address, which is not an acceptable format."),
\Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
);
$validator->setMessage(
__("'%value%' looks like a DNS hostname but we cannot match the TLD against known list."),
\Zend_Validate_Hostname::UNKNOWN_TLD
);
$validator->setMessage(
__("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
\Zend_Validate_Hostname::INVALID_DASH
);
$validator->setMessage(
__(
"'%value%' looks like a DNS hostname but we cannot match it against the hostname schema for TLD '%tld%'."
),
\Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA
);
$validator->setMessage(
__("'%value%' looks like a DNS hostname but cannot extract TLD part."),
\Zend_Validate_Hostname::UNDECIPHERABLE_TLD
);
$validator->setMessage(
__("'%value%' does not look like a valid local network name."),
\Zend_Validate_Hostname::INVALID_LOCAL_NAME
);
$validator->setMessage(
__("'%value%' looks like a local network name, which is not an acceptable format."),
\Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED
);
$validator->setMessage(
__(
"'%value%' appears to be a DNS hostname, but the given punycode notation cannot be decoded."
),
\Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE
);
$validator = new \Magento\Framework\Validator\EmailMinimal();
if (!$validator->isValid($value)) {
return array_unique($validator->getMessages());
return $validator->getMessages();
}
break;
case 'url':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ public function validateInputRuleDataProvider()
'email',
[
// @codingStandardsIgnoreStart
\Zend_Validate_EmailAddress::INVALID_HOSTNAME => '"mylabel" is not a valid hostname.',
\Zend_Validate_Hostname::INVALID_HOSTNAME => "'#\$' does not match the expected structure for a DNS hostname",
\Zend_Validate_Hostname::INVALID_LOCAL_NAME => "'#\$' does not look like a valid local network name."
// @codingStandardsIgnoreEnd
Expand Down
90 changes: 4 additions & 86 deletions app/code/Magento/Eav/Model/Attribute/Data/AbstractData.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,93 +353,11 @@ protected function _validateInputRule($value)
break;
case 'email':
/**
__("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded")
__("Invalid type given. String expected")
__("'%value%' appears to be a DNS hostname but contains a dash in an invalid position")
__("'%value%' does not match the expected structure for a DNS hostname")
__("'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'")
__("'%value%' does not appear to be a valid local network name")
__("'%value%' does not appear to be a valid URI hostname")
__("'%value%' appears to be an IP address but IP addresses are not allowed")
__("'%value%' appears to be a local network name but local network names are not allowed")
__("'%value%' appears to be a DNS hostname but cannot extract TLD part")
__("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
*/
$validator = new \Zend_Validate_EmailAddress();
$validator->setMessage(
__('"%1" invalid type entered.', $label),
\Zend_Validate_EmailAddress::INVALID
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::INVALID_FORMAT
);
$validator->setMessage(
__('"%1" is not a valid hostname.', $label),
\Zend_Validate_EmailAddress::INVALID_HOSTNAME
);
$validator->setMessage(
__('"%1" is not a valid hostname.', $label),
\Zend_Validate_EmailAddress::INVALID_MX_RECORD
);
$validator->setMessage(
__('"%1" is not a valid hostname.', $label),
\Zend_Validate_EmailAddress::INVALID_MX_RECORD
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::DOT_ATOM
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::QUOTED_STRING
);
$validator->setMessage(
__('"%1" is not a valid email address.', $label),
\Zend_Validate_EmailAddress::INVALID_LOCAL_PART
);
$validator->setMessage(
__('"%1" uses too many characters.', $label),
\Zend_Validate_EmailAddress::LENGTH_EXCEEDED
);
$validator->setMessage(
__("'%value%' looks like an IP address, which is not an acceptable format."),
\Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
);
$validator->setMessage(
__("'%value%' looks like a DNS hostname but we cannot match the TLD against known list."),
\Zend_Validate_Hostname::UNKNOWN_TLD
);
$validator->setMessage(
__("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
\Zend_Validate_Hostname::INVALID_DASH
);
$validator->setMessage(
__(
"'%value%' looks like a DNS hostname but we cannot match it against the hostname schema for TLD '%tld%'."
),
\Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA
);
$validator->setMessage(
__("'%value%' looks like a DNS hostname but cannot extract TLD part."),
\Zend_Validate_Hostname::UNDECIPHERABLE_TLD
);
$validator->setMessage(
__("'%value%' does not look like a valid local network name."),
\Zend_Validate_Hostname::INVALID_LOCAL_NAME
);
$validator->setMessage(
__("'%value%' looks like a local network name, which is not an acceptable format."),
\Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED
);
$validator->setMessage(
__(
"'%value%' appears to be a DNS hostname, but the given punycode notation cannot be decoded."
),
\Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE
);
__('"%1" is not a valid email address.')
*/
$validator = new \Magento\Framework\Validator\EmailMinimal();
if (!$validator->isValid($value)) {
return array_unique($validator->getMessages());
return $validator->getMessages();
}
break;
case 'url':
Expand Down
8 changes: 7 additions & 1 deletion app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ define([
},
$.validator.messages.email
],
'email-minimal': [
function (value) {
return this.optional(element) || value.indexOf('@') !== -1;
},
$.mage.__('Please enter a valid email address.')
],
'url2': [
function (value) {
return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|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value);//eslint-disable-line max-len
Expand Down Expand Up @@ -422,7 +428,7 @@ define([
],
'validate-email': [
function (value) {
return utils.isEmptyNoTrim(value) || /^([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*@([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*\.(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]){2,})$/i.test(value);//eslint-disable-line max-len
return utils.isEmptyNoTrim(value) || value.indexOf('@') !== -1;
},
$.mage.__('Please enter a valid email address (Ex: johndoe@domain.com).')
],
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/User/Model/UserValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Magento\User\Model;

use Magento\Framework\Validator\EmailAddress;
use Magento\Framework\Validator\EmailMinimal;
use Magento\Framework\Validator\NotEmpty;
use Magento\Framework\Validator\Regex;
use Magento\Framework\Validator\StringLength;
Expand Down Expand Up @@ -36,8 +36,8 @@ public function addUserInfoRules(\Magento\Framework\Validator\DataObject $valida
$firstNameNotEmpty->setMessage(__('First Name is a required field.'), \Zend_Validate_NotEmpty::IS_EMPTY);
$lastNameNotEmpty = new NotEmpty();
$lastNameNotEmpty->setMessage(__('Last Name is a required field.'), \Zend_Validate_NotEmpty::IS_EMPTY);
$emailValidity = new EmailAddress();
$emailValidity->setMessage(__('Please enter a valid email.'), \Zend_Validate_EmailAddress::INVALID);
$emailValidity = new EmailMinimal();
$emailValidity->setMessage(__('Please enter a valid email.'), \Magento\Framework\Validator\EmailMinimal::INVALID);

/** @var $validator \Magento\Framework\Validator\DataObject */
$validator->addRule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function testValidateActionError()
'username' => 'admin2',
'firstname' => 'new firstname',
'lastname' => 'new lastname',
'email' => 'example@domain.cim',
'email' => 'example.domain.com',
'password' => 'password123',
'password_confirmation' => 'password123',
];
Expand All @@ -245,6 +245,6 @@ public function testValidateActionError()
$body = $this->getResponse()->getBody();

$this->assertContains('{"error":1,"html_message":', $body);
$this->assertContains("'domain.cim' is not a valid hostname for email address 'example@domain.cim'", $body);
$this->assertContains('"example.domain.com" is not a valid email address.', $body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3632,6 +3632,7 @@
['Magento\ToolkitFramework\Helper\Cli'],
['Magento\ToolkitFramework\Config'],
['Magento\Framework\Locale\Validator', 'Magento\Framework\Validator\Locale'],
['Magento\Framework\Validator\EmailAddress', 'Magento\Framework\Validator\Email'],
['Magento\ToolkitFramework\Fixture', 'Magento\Setup\Fixtures\Fixture'],
['Magento\ToolkitFramework\Application', 'Magento\Setup\Fixtures\FixtureModel'],
['Magento\Framework\Locale\Lists', 'Magento\Framework\Locale\TranslatedLists'],
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/Magento/Framework/Validator/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* @deprecated
* @see \Magento\Framework\Validator\EmailMinimal
*/
namespace Magento\Framework\Validator;

class EmailAddress extends \Zend_Validate_EmailAddress implements \Magento\Framework\Validator\ValidatorInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to keep inheritance from Zend Framework 1 class. Please mark this class as @deprecated without changes and create a new one with proper logic.

As we don't have EmailAnything except EmailAddress, I believe simply Email would be a good name for such class.

Expand Down
89 changes: 89 additions & 0 deletions lib/internal/Magento/Framework/Validator/EmailMinimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Email address validator
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Validator;

class EmailMinimal implements \Zend_Validate_Interface
{
const INVALID = 'emailAddressInvalid';

/**
* Validation failure message template definitions
*
* @var string[]
*/
private $messageTemplates = [
self::INVALID => '"%1" is not a valid email address.'
];

/**
* Validation error messages
*
* @var string[] Validation error messages
*/
private $messages = [];

/**
* Validate email address contains '@' sign
*
* @param string $value
*
* @return bool
*/
public function isValid($value)
{
$this->messages = [];
if (!is_string($value) || strrpos($value, '@') === false) {
$translatedMessage = __($this->messageTemplates[self::INVALID], $value);
$this->messages[] = $translatedMessage;
}

return empty($this->_messages);
}

/**
* Return error messages (if any) after the last validation
*
* @return string[]
*/
public function getMessages()
{
return $this->messages;
}

/**
* Sets the validation failure message template for a particular key
*
* @param string $messageString
* @param null $messageKey OPTIONAL
*
* @return $this
* @throws \InvalidArgumentException If no message template exists for key
*/
public function setMessage($messageString, $messageKey = null)
{
if ($messageKey === null) {
$keys = array_keys($this->messageTemplates);
foreach ($keys as $key) {
$this->setMessage($messageString, $key);
}

return $this;
}

if (!isset($this->messageTemplates[$messageKey])) {
throw new \InvalidArgumentException(
sprintf('No message template exists for key %s', $messageKey)
);
}

$this->messageTemplates[$messageKey] = $messageString;

return $this;
}
}
Loading