mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #4117 from assertchris/clean-up-email-field
Clean up EmailField
This commit is contained in:
commit
57e39cec7b
@ -1,54 +1,59 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text input field with validation for correct email format
|
* Text input field with validation for correct email format according to RFC 2822.
|
||||||
* according to RFC 2822.
|
|
||||||
*
|
*
|
||||||
* @package forms
|
* @package forms
|
||||||
* @subpackage fields-formattedinput
|
* @subpackage fields-formattedinput
|
||||||
*/
|
*/
|
||||||
class EmailField extends TextField {
|
class EmailField extends TextField {
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function Type() {
|
public function Type() {
|
||||||
return 'email text';
|
return 'email text';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function getAttributes() {
|
public function getAttributes() {
|
||||||
return array_merge(
|
return array_merge(
|
||||||
parent::getAttributes(),
|
parent::getAttributes(),
|
||||||
array(
|
array(
|
||||||
'type' => 'email'
|
'type' => 'email',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates for RFC 2822 compliant email adresses.
|
* Validates for RFC 2822 compliant email addresses.
|
||||||
*
|
*
|
||||||
* @see http://www.regular-expressions.info/email.html
|
* @see http://www.regular-expressions.info/email.html
|
||||||
* @see http://www.ietf.org/rfc/rfc2822.txt
|
* @see http://www.ietf.org/rfc/rfc2822.txt
|
||||||
*
|
*
|
||||||
* @param Validator $validator
|
* @param Validator $validator
|
||||||
* @return String
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function validate($validator) {
|
public function validate($validator) {
|
||||||
$this->value = trim($this->value);
|
$this->value = trim($this->value);
|
||||||
|
|
||||||
$pcrePattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*'
|
$pattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$';
|
||||||
. '@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$';
|
|
||||||
|
|
||||||
// PHP uses forward slash (/) to delimit start/end of pattern, so it must be escaped
|
// Escape delimiter characters.
|
||||||
$pregSafePattern = str_replace('/', '\\/', $pcrePattern);
|
$safePattern = str_replace('/', '\\/', $pattern);
|
||||||
|
|
||||||
if($this->value && !preg_match('/' . $pregSafePattern . '/i', $this->value)){
|
if($this->value && !preg_match('/' . $safePattern . '/i', $this->value)) {
|
||||||
$validator->validationError(
|
$validator->validationError(
|
||||||
$this->name,
|
$this->name,
|
||||||
_t('EmailField.VALIDATION', "Please enter an email address"),
|
_t('EmailField.VALIDATION', 'Please enter an email address'),
|
||||||
"validation"
|
'validation'
|
||||||
);
|
);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} else{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user