1 <?php
2
3 4 5 6 7 8
9 class ValidationResult extends Object {
10 11 12
13 protected $isValid;
14
15
16 17 18
19 protected $errorList = array();
20
21 22 23 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 33 34 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 53
54 function valid() {
55 return $this->isValid;
56 }
57
58 59 60
61 function messageList() {
62 return $this->errorList;
63 }
64
65 66 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 76
77 function message() {
78 return implode("; ", $this->errorList);
79 }
80
81 82 83
84 function starredList() {
85 return " * " . implode("\n * ", $this->errorList);
86 }
87
88 89 90 91 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.
-