[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/form/rules/ -> email.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Form
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  /**
  13   * Form Rule class for the Joomla Platform.
  14   *
  15   * @package     Joomla.Platform
  16   * @subpackage  Form
  17   * @since       11.1
  18   */
  19  class JFormRuleEmail extends JFormRule
  20  {
  21      /**
  22       * The regular expression to use in testing a form field value.
  23       *
  24       * @var    string
  25       * @since  11.1
  26       */
  27      protected $regex = '^[\w.-]+(\+[\w.-]+)*@\w+[\w.-]*?\.\w{2,4}$';
  28  
  29      /**
  30       * Method to test the email address and optionally check for uniqueness.
  31       *
  32       * @param   JXMLElement  &$element  The JXMLElement object representing the <field /> tag for the form field object.
  33       * @param   mixed        $value     The form field value to validate.
  34       * @param   string       $group     The field name group control value. This acts as as an array container for the field.
  35       *                                  For example if the field has name="foo" and the group value is set to "bar" then the
  36       *                                  full field name would end up being "bar[foo]".
  37       * @param   JRegistry    &$input    An optional JRegistry object with the entire data set to validate against the entire form.
  38       * @param   object       &$form     The form object for which the field is being tested.
  39       *
  40       * @return  boolean  True if the value is valid, false otherwise.
  41       *
  42       * @since   11.1
  43       * @throws  JException on invalid rule.
  44       */
  45  	public function test(&$element, $value, $group = null, &$input = null, &$form = null)
  46      {
  47          // If the field is empty and not required, the field is valid.
  48          $required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
  49          if (!$required && empty($value))
  50          {
  51              return true;
  52          }
  53  
  54          // Test the value against the regular expression.
  55          if (!parent::test($element, $value, $group, $input, $form))
  56          {
  57              return false;
  58          }
  59  
  60          // Check if we should test for uniqueness.
  61          $unique = ((string) $element['unique'] == 'true' || (string) $element['unique'] == 'unique');
  62          if ($unique)
  63          {
  64  
  65              // Get the database object and a new query object.
  66              $db = JFactory::getDBO();
  67              $query = $db->getQuery(true);
  68  
  69              // Build the query.
  70              $query->select('COUNT(*)');
  71              $query->from('#__users');
  72              $query->where('email = ' . $db->quote($value));
  73  
  74              // Get the extra field check attribute.
  75              $userId = ($form instanceof JForm) ? $form->getValue('id') : '';
  76              $query->where($db->quoteName('id') . ' <> ' . (int) $userId);
  77  
  78              // Set and query the database.
  79              $db->setQuery($query);
  80              $duplicate = (bool) $db->loadResult();
  81  
  82              // Check for a database error.
  83              if ($db->getErrorNum())
  84              {
  85                  JError::raiseWarning(500, $db->getErrorMsg());
  86              }
  87  
  88              if ($duplicate)
  89              {
  90                  return false;
  91              }
  92          }
  93  
  94          return true;
  95      }
  96  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1