2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-04-27 05:38:34 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2015-04-27 05:38:34 +02:00
|
|
|
* Text input field with validation for correct email format according to RFC 2822.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class EmailField extends TextField {
|
2015-04-27 05:38:34 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return 'email text';
|
|
|
|
}
|
|
|
|
|
2015-04-27 05:38:34 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAttributes() {
|
2012-04-19 04:26:03 +02:00
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array(
|
2015-04-27 05:38:34 +02:00
|
|
|
'type' => 'email',
|
2012-04-19 04:26:03 +02:00
|
|
|
)
|
2012-04-19 04:06:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-10-19 06:57:52 +02:00
|
|
|
/**
|
2015-04-27 05:38:34 +02:00
|
|
|
* Validates for RFC 2822 compliant email addresses.
|
|
|
|
*
|
2010-10-19 06:57:52 +02:00
|
|
|
* @see http://www.regular-expressions.info/email.html
|
|
|
|
* @see http://www.ietf.org/rfc/rfc2822.txt
|
2015-04-27 05:38:34 +02:00
|
|
|
*
|
2010-10-19 06:57:52 +02:00
|
|
|
* @param Validator $validator
|
2015-04-27 05:38:34 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2010-10-19 06:57:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function validate($validator) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->value = trim($this->value);
|
2010-10-19 06:57:52 +02:00
|
|
|
|
2015-04-27 05:38:34 +02:00
|
|
|
$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])?$';
|
2010-10-19 06:57:52 +02:00
|
|
|
|
2015-04-27 05:38:34 +02:00
|
|
|
// Escape delimiter characters.
|
|
|
|
$safePattern = str_replace('/', '\\/', $pattern);
|
2010-10-19 06:57:52 +02:00
|
|
|
|
2015-04-27 05:38:34 +02:00
|
|
|
if($this->value && !preg_match('/' . $safePattern . '/i', $this->value)) {
|
2012-04-12 02:17:30 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
2015-04-27 05:38:34 +02:00
|
|
|
_t('EmailField.VALIDATION', 'Please enter an email address'),
|
|
|
|
'validation'
|
2007-10-25 04:47:45 +02:00
|
|
|
);
|
2015-04-27 05:38:34 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2015-04-27 05:38:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|