Clean up EmailField

This commit is contained in:
Christopher Pitt 2015-04-27 15:38:34 +12:00
parent 72ee96cd65
commit 7963a0b870

View File

@ -1,54 +1,59 @@
<?php
/**
* Text input field with validation for correct email format
* according to RFC 2822.
*
* Text input field with validation for correct email format according to RFC 2822.
*
* @package forms
* @subpackage fields-formattedinput
*/
class EmailField extends TextField {
/**
* {@inheritdoc}
*/
public function Type() {
return 'email text';
}
/**
* {@inheritdoc}
*/
public function getAttributes() {
return array_merge(
parent::getAttributes(),
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.ietf.org/rfc/rfc2822.txt
*
*
* @param Validator $validator
* @return String
*
* @return string
*/
public function validate($validator) {
$this->value = trim($this->value);
$pcrePattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*'
. '@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[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])?$';
// PHP uses forward slash (/) to delimit start/end of pattern, so it must be escaped
$pregSafePattern = str_replace('/', '\\/', $pcrePattern);
// Escape delimiter characters.
$safePattern = str_replace('/', '\\/', $pattern);
if($this->value && !preg_match('/' . $pregSafePattern . '/i', $this->value)){
if($this->value && !preg_match('/' . $safePattern . '/i', $this->value)) {
$validator->validationError(
$this->name,
_t('EmailField.VALIDATION', "Please enter an email address"),
"validation"
_t('EmailField.VALIDATION', 'Please enter an email address'),
'validation'
);
return false;
} else{
return true;
}
}
return false;
}
return true;
}
}