API16

Difference between revisions of "JFormRuleUsername/test"

From Joomla! Documentation

< API16:JFormRuleUsername
(New page: ===Description=== Method to test if a username is unique. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<no...)
 
Line 17: Line 17:
 
!Description
 
!Description
 
|-
 
|-
|  
+
| &$field
 
|  
 
|  
 
|  $field A reference to the form field.  
 
|  $field A reference to the form field.  
 
|-
 
|-
|  
+
| &$values
 
|  
 
|  
 
|  $values The values to test for validiaty.  
 
|  $values The values to test for validiaty.  

Revision as of 05:15, 30 March 2010

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Method to test if a username is unique.

[Edit Descripton]

Template:Description:JFormRuleUsername/test

Syntax[edit]

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[edit]

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

Defined in[edit]

libraries/joomla/form/rules/username.php

Importing[edit]

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

Source Body[edit]

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;
}

[Edit See Also] Template:SeeAlso:JFormRuleUsername/test

Examples[edit]

<CodeExamplesForm />