API16

JFormRuleUsername/test: Difference between revisions

From Joomla! Documentation

m clean up
m preparing for archive only
 
Line 72: Line 72:


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=test
  category=test
  category=JFormRuleUsername
  category=JFormRuleUsername
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*

Latest revision as of 01:43, 25 March 2017

Description

Method to test if a username is unique.


<! removed transcluded page call, red link never existed >

Syntax

test(&$field, &$values)
Parameter Name Default Value Description
&$field $field A reference to the form field.
&$values $values The values to test for validiaty.

Returns

mixed on invalid rule, true if the value is valid, false otherwise.

Defined in

libraries/joomla/form/rules/username.php

Importing

jimport( 'joomla.form.rules.username' );

Source Body

public function test(&$field, &$values)
{
        $return = false;
        $name   = (string)$field->attributes()->name;
        $key    = (string)$field->attributes()->field;
        $value  = isset($values[$key]) ? $values[$key] : 0;

        // Check the rule.
        if (!$key) {
                return new JException('Invalid Form Rule :: '.get_class($this));
        }

        // Check if the username is unique.
        $db = &JFactory::getDbo();
        $db->setQuery(
                'SELECT count(*) FROM `#__users`' .
                ' WHERE `username` = '.$db->Quote($values[$name]) .
                ' AND '.$db->nameQuote($key).' != '.$db->Quote($value)
        );
        $duplicate = (bool)$db->loadResult();

        // Check for a database error.
        if ($db->getErrorNum()) {
                return new JException('Database Error :: '.$db->getErrorMsg());
        }

        if (!$duplicate) {
                $return = true;
        }

        return $return;
}


<! removed transcluded page call, red link never existed >

Examples

Code Examples