*/ WBClass::load('WBPAT_Forms_Rule'); /** * patForms Rule Duplicate * * Helps to avoid duplicate email and nicknames in user tables for example * * @version 0.2.0 * @package Wombat * @subpackage patForms */ class patForms_Rule_TableDuplicate extends WBPAT_Forms_Rule { /** * default config values * - table * - column * - id * * @var array */ protected $config = array( 'table' => 'user', 'column' => 'nickname', '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', 'This 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; } $table = WBClass::create('WBDatasource_Table'); /** @var $table WBDatasource_Table */ $clause = array(); $clause[] = array( 'field' => $this->config['column'], 'value' => $value ); $others = $table->getIds($this->config['table'], null, $clause); // no duplicates found, all good if (0 == count($others)) { return true; } // is this me? if (!empty($this->config['id']) && 1 == count($others) && $others[0] == $this->config['id']) { return true; } $this->addValidationError(2); return false; } } ?>