2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Text field with Email Validation.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class EmailField extends TextField {
|
2011-12-21 17:35:42 +01:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
function Type() {
|
|
|
|
return 'email text';
|
|
|
|
}
|
|
|
|
|
2012-04-19 04:06:47 +02:00
|
|
|
function getAttributes() {
|
2012-04-19 04:26:03 +02:00
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array(
|
|
|
|
'type' => 'email'
|
|
|
|
)
|
2012-04-19 04:06:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-10-19 06:57:52 +02:00
|
|
|
/**
|
|
|
|
* Validates for RFC 2822 compliant email adresses.
|
|
|
|
*
|
|
|
|
* @see http://www.regular-expressions.info/email.html
|
|
|
|
* @see http://www.ietf.org/rfc/rfc2822.txt
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-04-12 02:17:30 +02:00
|
|
|
function validate($validator) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->value = trim($this->value);
|
2010-10-19 06:57:52 +02:00
|
|
|
|
2012-04-12 02:17:30 +02:00
|
|
|
$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])?$';
|
2010-10-19 06:57:52 +02:00
|
|
|
|
|
|
|
// PHP uses forward slash (/) to delimit start/end of pattern, so it must be escaped
|
|
|
|
$pregSafePattern = str_replace('/', '\\/', $pcrePattern);
|
|
|
|
|
|
|
|
if($this->value && !preg_match('/' . $pregSafePattern . '/i', $this->value)){
|
2012-04-12 02:17:30 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
2012-06-01 03:13:06 +02:00
|
|
|
_t('EmailField.VALIDATION', "Please enter an email address"),
|
2007-10-25 04:47:45 +02:00
|
|
|
"validation"
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
} else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|