*/ WBClass::load('WBPAT_Forms_Rule'); /** * patForms Rule Duplicate * * Helps to avoid duplicate email and nicknames in user tables for example * * @version 0.1.0 * @package Wombat * @subpackage patForms */ class patForms_Rule_EmailDuplicate extends WBPAT_Forms_Rule { /** * default config values * - table * - id * * @var array */ protected $config = array( 'table' => 'user', 'id' => null ); /** * Initialize validation codes using gettext * * @access protected * @return bool $success Always returns true. * @see $attributeDefaults */ public function loadValidatiorErrorCodes() { $this->validatorErrorCodes = array( 1 => patI18n::dgettext('wombat', 'The entered value must not be empty.'), 2 => patI18n::dgettext('wombat', 'The @ sign is missing.'), 3 => patI18n::dgettext('wombat', 'This email address is already in use, try another one..'), ); return true; } /** * method called by patForms or any patForms_Element to validate the * element or the form. * * @param patForms_Element $form object * @return bool true on sucess */ public function applyRule(&$element, $type = PATFORMS_RULE_AFTER_VALIDATION) { $value = $element->getValue(); if (empty($value)) { $this->addValidationError(1); return false; } // check for @ if (strstr($value, '@') === false) { $this->addValidationError(2); return false; } list($local, $domain) = explode('@', $value, 2); $table = WBClass::create('WBDatasource_Table'); /** @var $table WBDatasource_Table */ $clause = array(); $clause[] = array( 'field' => 'emaillocal', 'value' => $local ); $clause[] = array( 'field' => 'emaildomain', 'value' => $domain ); if ($this->config['id']) { $clause[] = array( 'field' => $table->getIdentifier($this->config['table']), 'relation' => 'not', 'value' => $this->config['id'] ); } $others = $table->getIds($this->config['table'], null, $clause); if (count($others)) { $this->addValidationError(3); return false; } return true; } } ?>