[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2020 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  4.0.0
  27   */
  28  class UserIdRule extends FormRule implements DatabaseAwareInterface
  29  {
  30      use DatabaseAwareTrait;
  31  
  32      /**
  33       * Method to test the validity of a Joomla User.
  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   4.0.0
  46       */
  47      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  48      {
  49          // Check if the field is required.
  50          $required = ((string) $element['required'] === 'true' || (string) $element['required'] === 'required');
  51  
  52          // If the value is empty, null or has the value 0 and the field is not required return true else return false
  53          if (($value === '' || $value === null || (string) $value === '0')) {
  54              return !$required;
  55          }
  56  
  57          // Get the database object and a new query object.
  58          $db    = $this->getDatabase();
  59          $query = $db->getQuery(true);
  60  
  61          // Build the query.
  62          $query->select('COUNT(*)')
  63              ->from($db->quoteName('#__users'))
  64              ->where($db->quoteName('id') . ' = :userId')
  65              ->bind(':userId', $value, ParameterType::INTEGER);
  66  
  67          // Set and query the database.
  68          return (bool) $db->setQuery($query)->loadResult();
  69      }
  70  }


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