[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Rule/ -> UsernameRule.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Rule;
  11  
  12  use Joomla\CMS\Form\Form;
  13  use Joomla\CMS\Form\FormRule;
  14  use Joomla\Database\DatabaseAwareInterface;
  15  use Joomla\Database\DatabaseAwareTrait;
  16  use Joomla\Database\ParameterType;
  17  use Joomla\Registry\Registry;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Form Rule class for the Joomla Platform.
  25   *
  26   * @since  1.7.0
  27   */
  28  class UsernameRule extends FormRule implements DatabaseAwareInterface
  29  {
  30      use DatabaseAwareTrait;
  31  
  32      /**
  33       * Method to test the username for uniqueness.
  34       *
  35       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  36       * @param   mixed              $value    The form field value to validate.
  37       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  38       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  39       *                                       full field name would end up being "bar[foo]".
  40       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  41       * @param   Form               $form     The form object for which the field is being tested.
  42       *
  43       * @return  boolean  True if the value is valid, false otherwise.
  44       *
  45       * @since   1.7.0
  46       */
  47      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  48      {
  49          // Get the database object and a new query object.
  50          $db    = $this->getDatabase();
  51          $query = $db->getQuery(true);
  52  
  53          // Get the extra field check attribute.
  54          $userId = ($form instanceof Form) ? (int) $form->getValue('id') : 0;
  55  
  56          // Build the query.
  57          $query->select('COUNT(*)')
  58              ->from($db->quoteName('#__users'))
  59              ->where(
  60                  [
  61                      $db->quoteName('username') . ' = :username',
  62                      $db->quoteName('id') . ' <> :userId',
  63                  ]
  64              )
  65              ->bind(':username', $value)
  66              ->bind(':userId', $userId, ParameterType::INTEGER);
  67  
  68          // Set and query the database.
  69          $db->setQuery($query);
  70          $duplicate = (bool) $db->loadResult();
  71  
  72          if ($duplicate) {
  73              return false;
  74          }
  75  
  76          return true;
  77      }
  78  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer