API16

JFormRuleEmail/test: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
No edit summary
m preparing for archive only
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
Method to test if an e-mail address is unique.
Method to test if an e-mail address is unique.


<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JFormRuleEmail/test|Edit Descripton]]<nowiki>]</nowiki>
</span>


{{Description:JFormRuleEmail/test}}
 
<! removed transcluded page call, red link never existed >


===Syntax===
===Syntax===
Line 92: Line 90:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[</nowiki>[[SeeAlso:JFormRuleEmail/test|Edit See Also]]<nowiki>]</nowiki>
<! removed transcluded page call, red link never existed >
</span>
{{SeeAlso:JFormRuleEmail/test}}


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=test
  category=test
  category=JFormRuleEmail
  category=JFormRuleEmail
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>
[[Category:Archived pages API16]]

Latest revision as of 01:43, 25 March 2017

Description

Method to test if an e-mail address 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/email.php

Importing

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

Source Body

public function test(&$field, &$values)
{
        $return = false;
        $name   = (string)$field->attributes()->name;
        $check  = ((string)$field->attributes()->unique == 'true' || (string)$field->attributes()->unique == 'unique');

        // If the field is empty and not required, the field is valid.
        if ((string)$field->attributes()->required != 'true') {
                // Get the data for the field.
                $value = array_key_exists($name, $values) ? $values[$name] : null;

                // If the data is empty, return valid.
                if ($value == null) {
                        return true;
                }
        }

        // Check if we should test for uniqueness.
        if ($check) {
                $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 `email` = '.$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());
                }

                // Test the value against the regular expression.
                if (parent::test($field, $values) && !$duplicate) {
                        $return = true;
                }
        } else {
                // Test the value against the regular expression.
                if (parent::test($field, $values)) {
                        $return = true;
                }
        }

        return $return;
}


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

Examples

Code Examples