/libraries/joomla/form/rule/username.php

https://gitlab.com/vitaliylukin91/text · PHP · 62 lines · 23 code · 8 blank · 31 comment · 1 complexity · 003d0dedba8375b54dbbbe691d6c35bc MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. use Joomla\Registry\Registry;
  11. /**
  12. * Form Rule class for the Joomla Platform.
  13. *
  14. * @since 11.1
  15. */
  16. class JFormRuleUsername extends JFormRule
  17. {
  18. /**
  19. * Method to test the username for uniqueness.
  20. *
  21. * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
  22. * @param mixed $value The form field value to validate.
  23. * @param string $group The field name group control value. This acts as as an array container for the field.
  24. * For example if the field has name="foo" and the group value is set to "bar" then the
  25. * full field name would end up being "bar[foo]".
  26. * @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
  27. * @param JForm $form The form object for which the field is being tested.
  28. *
  29. * @return boolean True if the value is valid, false otherwise.
  30. *
  31. * @since 11.1
  32. */
  33. public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
  34. {
  35. // Get the database object and a new query object.
  36. $db = JFactory::getDbo();
  37. $query = $db->getQuery(true);
  38. // Build the query.
  39. $query->select('COUNT(*)')
  40. ->from('#__users')
  41. ->where('username = ' . $db->quote($value));
  42. // Get the extra field check attribute.
  43. $userId = ($form instanceof JForm) ? $form->getValue('id') : '';
  44. $query->where($db->quoteName('id') . ' <> ' . (int) $userId);
  45. // Set and query the database.
  46. $db->setQuery($query);
  47. $duplicate = (bool) $db->loadResult();
  48. if ($duplicate)
  49. {
  50. return false;
  51. }
  52. return true;
  53. }
  54. }