silverstripe-framework/forms/EmailField.php

55 lines
1.2 KiB
PHP
Raw Normal View History

<?php
/**
2012-06-26 15:03:11 +02:00
* Text input field with validation for correct email format
* according to RFC 2822.
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage fields-formattedinput
*/
class EmailField extends TextField {
public function Type() {
return 'email text';
}
public function getAttributes() {
2012-04-19 04:26:03 +02:00
return array_merge(
parent::getAttributes(),
array(
'type' => 'email'
)
);
}
/**
* Validates for RFC 2822 compliant email adresses.
2014-08-15 08:53:05 +02:00
*
* @see http://www.regular-expressions.info/email.html
* @see http://www.ietf.org/rfc/rfc2822.txt
2014-08-15 08:53:05 +02:00
*
* @param Validator $validator
* @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])?$';
// 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,
_t('EmailField.VALIDATION', "Please enter an email address"),
"validation"
);
return false;
} else{
return true;
}
}
}