Version: current

     1 <?php
     2
     3 /**
     4  * A class that combined as a boolean result with an optional list of error messages.
     5  * This is used for returning validation results from validators
     6  * @package sapphire
     7  * @subpackage core
     8  */
     9 class ValidationResult extends Object {
    10     /**
    11      * Boolean - is the result valid or not
    12      */
    13     protected $isValid;
    14
    15
    16     /**
    17      * Array of errors
    18      */
    19     protected $errorList = array();
    20
    21     /**
    22      * Create a new ValidationResult.
    23      * By default, it is a successful result.   Call $this->error() to record errors.
    24      */
    25     function __construct($valid = true, $message = null) {
    26         $this->isValid = $valid;
    27         if($message) $this->errorList[] = $message;
    28         parent::__construct();
    29     }
    30
    31     /**
    32      * Record an error against this validation result,
    33      * @param $message The validation error message
    34      * @param $code An optional error code string, that can be accessed with {@link $this->codeList()}.
    35      */
    36     function error($message, $code = null) {
    37         $this->isValid = false;
    38
    39         if($code) {
    40             if(!is_numeric($code)) {
    41                 $this->errorList[$code] = $message;
    42             } else {
    43                 user_error("ValidationResult::error() - Don't use a numeric code '$code'.  Use a string.  I'm going to ignore it.", E_USER_WARNING);
    44                 $this->errorList[$code] = $message;
    45             }
    46         } else {
    47             $this->errorList[] = $message;
    48         }
    49     }
    50
    51     /**
    52      * Returns true if the result is valid.
    53      */
    54     function valid() {
    55         return $this->isValid;
    56     }
    57
    58     /**
    59      * Get an array of errors
    60      */
    61     function messageList() {
    62         return $this->errorList;
    63     }
    64
    65     /**
    66      * Get an array of error codes
    67      */
    68     function codeList() {
    69         $codeList = array();
    70         foreach($this->errorList as $k => $v) if(!is_numeric($k)) $codeList[] = $k;
    71         return $codeList;
    72     }
    73
    74     /**
    75      * Get the error message as a string.
    76      */
    77     function message() {
    78         return implode("; ", $this->errorList);
    79     }
    80
    81     /**
    82      * Get a starred list of all messages
    83      */
    84     function starredList() {
    85         return " * " . implode("\n * ", $this->errorList);
    86     }
    87
    88     /**
    89      * Combine this Validation Result with the ValidationResult given in other.
    90      * It will be valid if both this and the other result are valid.
    91      * This object will be modified to contain the new validation information.
    92      */
    93     function combineAnd(ValidationResult $other) {
    94         $this->isValid = $this->isValid && $other->valid();
    95         $this->errorList = array_merge($this->errorList, $other->messageList());
    96     }
    97
    98
    99 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -