silverstripe-framework/core/ValidationResult.php
Sam Minnee eb60b67732 Merged revisions 52121 via svnmerge from
http://svn.silverstripe.com/open/modules/sapphire/branches/govtsecurity

........
  r52121 | sminnee | 2008-04-03 22:04:33 +1300 (Thu, 03 Apr 2008) | 4 lines
  
  Added DataObject::validate() for specifying DataObject-level validators.
  Added DataObject::onAfterWrite(), a complement of DataObject::onBeforeWrite()
  Added password strength testing to security system
  Added password expiry to security system
........


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@53465 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-04-26 06:31:52 +00:00

99 lines
2.3 KiB
PHP

<?php
/**
* A class that combined as a boolean result with an optional list of error messages.
* This is used for returning validation results from validators
*/
class ValidationResult extends Object {
/**
* Boolean - is the result valid or not
*/
protected $isValid;
/**
* Array of errors
*/
protected $errorList = array();
/**
* Create a new ValidationResult.
* By default, it is a successful result. Call $this->error() to record errors.
*/
function __construct($valid = true, $message = null) {
$this->isValid = true;
if($message) $this->errorList[] = $message;
}
/**
* Record an error against this validation result,
* @param $message The validation error message
* @param $code An optional error code string, that can be accessed with {@link $this->codeList()}.
*/
function error($message, $code = null) {
$message = trim($message);
if(substr($message,-1) == '.') $message = substr($message,0,-1);
$this->isValid = false;
if($code) {
if(!is_numeric($code)) {
$this->errorList[$code] = $message;
} else {
user_error("ValidationResult::error() - Don't use a numeric code '$code'. Use a string. I'm going to ignore it.", E_USER_WARNING);
$this->errorList[$code] = $message;
}
} else {
$this->errorList[] = $message;
}
}
/**
* Returns true if the result is valid.
*/
function valid() {
return $this->isValid;
}
/**
* Get an array of errors
*/
function messageList() {
return $this->errorList;
}
/**
* Get an array of error codes
*/
function codeList() {
$codeList = array();
foreach($this->errorList as $k => $v) if(!is_numeric($k)) $codeList[] = $k;
return $codeList;
}
/**
* Get the error message as a string.
*/
function message() {
return implode("; ", $this->errorList);
}
/**
* Get a starred list of all messages
*/
function starredList() {
return " * " . implode("\n * ", $this->errorList);
}
/**
* Combine this Validation Result with the ValidationResult given in other.
* It will be valid if both this and the other result are valid.
* This object will be modified to contain the new validation information.
*/
function combineAnd(ValidationResult $other) {
$this->isValid = $this->isValid && $other->valid();
$this->errorList = array_merge($this->errorList, $other->messageList());
}
}