tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file form_field_email.class.php

Documentation is available at form_field_email.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Form_Field_Email
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage form
  11.  */
  12.  
  13. /**
  14.  *
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'form/fields/text/form_field_text.class.php');
  17. /**
  18.  *
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'misc/inet_functions.inc.php');
  21. /**
  22.  *
  23.  */
  24. require_once(TANGRA_MAIN_DIR.'exceptions/te_invalid_email.class.php');
  25.  
  26.  
  27. /**
  28.  * Represents text field that accepts email
  29.  *
  30.  * @package tangra_lib
  31.  * @subpackage form
  32.  */
  33. class Form_Field_Email extends Form_Field_Text {
  34.     /**
  35.      * Constructor
  36.      *
  37.      * @param string $name Name of the field
  38.      * @param boolean $required Is field required. Default is false
  39.      * @param integer $maxlength Maximal length of the text. Default is 100
  40.      * @param string $value Default value.  Must be valid email. Default is NULL
  41.      */
  42.     function __construct($name$required false$maxlength 100$value NULL{
  43.         parent::__construct($name$required$maxlength$value);
  44.  
  45.         $this->add_potential_error('invalid_email');
  46.     }
  47.  
  48.  
  49.     /**
  50.      * Sets field value. Must be valid email address or empty (NULL/'')
  51.      *
  52.      * @param string $value 
  53.      * @throws  TE_Invalid_Email
  54.      */
  55.     function set_value($value{
  56.         if ($value{
  57.             $value trim($value);
  58.             if (is_valid_email($value)) {
  59.                 parent::set_value($value);
  60.             else {
  61.                 throw new TE_Invalid_Email($value);
  62.             }
  63.         else {
  64.             parent::set_value($value);
  65.         }
  66.     }
  67.  
  68.  
  69.     /**
  70.      * Performs basic check for validity
  71.      *
  72.      * @return boolean 
  73.      * @internal
  74.      */
  75.     public function basic_check({
  76.         $has_errors parent::basic_check();
  77.  
  78.         if (!$has_errors{
  79.             if ($this->get_html_value()) {
  80.                 if (!is_valid_email($this->get_html_value())) {
  81.                     $this->set_error('invalid_email');
  82.                     $has_errors true;
  83.                 }
  84.             }
  85.         }
  86.  
  87.         return $has_errors;
  88.     }
  89.  
  90. }