Blame view

framework/forms/EmailField.php 1.24 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  <?php
  /**
   * Text input field with validation for correct email format
   * according to RFC 2822.
   * 
   * @package forms
   * @subpackage fields-formattedinput
   */
  class EmailField extends TextField {
  
  	public function Type() {
  		return 'email text';
  	}
  
  	public function getAttributes() {
  		return array_merge(
  			parent::getAttributes(),
  			array(
  				'type' => 'email'
  			)
  		);
  	}
  
  	/**
  	 * 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
  	 */
  	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)){
  			$validator->validationError(
  				$this->name,
  				_t('EmailField.VALIDATION', "Please enter an email address"),
  				"validation"
  			);
  			return false;
  		} else{
  			return true;
  		}
  	}
  
  }