Framework  3.9
UniqueValidator Class Reference

Uniqueness Validator. More...

+ Inheritance diagram for UniqueValidator:
+ Collaboration diagram for UniqueValidator:

Public Member Functions

 UniqueValidator ($field, $title, $item, $message, $extraConstraint="")
 
 writeClient ()
 
 validate ()
 
- Public Member Functions inherited from AbstractValidator
 AbstractValidator ($field, $title)
 

Public Attributes

 $item
 
 $message
 
- Public Attributes inherited from AbstractValidator
 $field
 
 $title
 

Detailed Description

Uniqueness Validator.

Tests to ensure that the database does not contain a record which matches the supplied value for the given field (useful for usernames, etc.) Obviously, this validator has no client-side component.

Definition at line 741 of file validation.inc.

Member Function Documentation

◆ UniqueValidator()

UniqueValidator::UniqueValidator (   $field,
  $title,
  $item,
  $message,
  $extraConstraint = "" 
)

Definition at line 746 of file validation.inc.

747  {
748  $this->class = $item->table;
749 
750  if(!$message)
751  {
752  $className = $item->prettifyClassName();
753  $message = (preg_match("/^[aeiou]/i", $className)) ? "An " : "A ";
754  $fieldName = strtolower($title);
755  $message .= "{$className} already exists with that {$fieldName}. Please choose a different {$fieldName}.";
756  }
757  $this->message = $message;
758  $this->item =& $item;
759  $this->extraConstraint = $extraConstraint;
760 
762  }
AbstractValidator($field, $title)
Definition: validation.inc:53

◆ validate()

UniqueValidator::validate ( )

Reimplemented from AbstractValidator.

Definition at line 771 of file validation.inc.

772  {
773  global $_POST;
774 
775  $value = $_POST[$this->field];
776 
777  $result = "";
778 
779  if ($value)
780  {
781  trace("UniqueValidator::validate: ".get_class($this->item), 3);
782 
783  $value = ConnectionManager::quote($value);
784 
785  $pk = $this->item->primary_key;
786 
787  $constraint = "WHERE {$this->field}=$value";
788 
789  if ($this->extraConstraint) $constraint .= " AND {$this->extraConstraint}";
790 
791  if ($this->item->$pk)
792  {
793  $constraint .= " AND {$pk}!='{$this->item->$pk}'";
794  }
795 
796  $matches = $this->item->queryValue("COUNT(1)", $constraint);
797  trace("***************count matches ". count($matches), 3);
798  if ($matches > 0)
799  {
800  trace("UniqueValidator::validate - found $matches match(es), raising error", 3);
801 
802  $result = $this->message;
803  }
804  }
805 
806  return $result;
807  }
static quote($str)
Quote a string value based on the character set of the global connection.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ writeClient()

UniqueValidator::writeClient ( )

Reimplemented from AbstractValidator.

Definition at line 764 of file validation.inc.

765  {
766  // No client-side component
767 
768  return "";
769  }

Member Data Documentation

◆ $item

UniqueValidator::$item

Definition at line 743 of file validation.inc.

◆ $message

UniqueValidator::$message

Definition at line 744 of file validation.inc.


The documentation for this class was generated from the following file: