[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2018 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\Registry\Registry;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Form rule class to determine if a value exists in a database table.
  24   *
  25   * @since  3.9.0
  26   */
  27  class ExistsRule extends FormRule implements DatabaseAwareInterface
  28  {
  29      use DatabaseAwareTrait;
  30  
  31      /**
  32       * Method to test the username for uniqueness.
  33       *
  34       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  35       * @param   mixed              $value    The form field value to validate.
  36       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  37       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  38       *                                       full field name would end up being "bar[foo]".
  39       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  40       * @param   Form               $form     The form object for which the field is being tested.
  41       *
  42       * @return  boolean  True if the value is valid, false otherwise.
  43       *
  44       * @since   3.9.0
  45       */
  46      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  47      {
  48          $value = trim($value);
  49  
  50          $existsTable  = (string) $element['exists_table'];
  51          $existsColumn = (string) $element['exists_column'];
  52  
  53          // We cannot validate without a table name
  54          if ($existsTable === '') {
  55              return true;
  56          }
  57  
  58          // Assume a default column name of `id`
  59          if ($existsColumn === '') {
  60              $existsColumn = 'id';
  61          }
  62  
  63          $db = $this->getDatabase();
  64  
  65          // Set and query the database.
  66          $exists = $db->setQuery(
  67              $db->getQuery(true)
  68                  ->select('COUNT(*)')
  69                  ->from($db->quoteName($existsTable))
  70                  ->where($db->quoteName($existsColumn) . ' = ' . $db->quote($value))
  71          )->loadResult();
  72  
  73          return (int) $exists > 0;
  74      }
  75  }


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