1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 abstract class Validator extends Object {
17
18 19 20
21 protected $form;
22
23 24 25
26 protected $errors;
27
28 29 30 31 32
33 protected static $javascript_validation_handler = "prototype";
34
35 36 37 38
39 protected $javascriptValidationHandler = null;
40
41 42 43 44 45 46 47 48 49 50
51 public static function set_javascript_validation_handler($handler) {
52 if($handler == 'prototype' || $handler == 'none') {
53 self::$javascript_validation_handler = $handler;
54 } else {
55 user_error("Validator::setJavascriptValidationHandler() passed bad handler '$handler'", E_USER_WARNING);
56 }
57 }
58
59 60 61 62 63 64
65 public static function get_javascript_validator_handler() {
66 return self::$javascript_validation_handler;
67 }
68
69 70 71 72 73 74 75
76 public function setJavascriptValidationHandler($handler) {
77 if($handler == 'prototype' || $handler == 'none') {
78 $this->javascriptValidationHandler = $handler;
79 } else {
80 user_error("Validator::setJavascriptValidationHandler() passed bad handler '$handler'", E_USER_WARNING);
81 }
82 }
83
84 85 86 87 88 89
90 public function getJavascriptValidationHandler() {
91 return ($this->javascriptValidationHandler) ? $this->javascriptValidationHandler : self::$javascript_validation_handler;
92 }
93
94 95 96
97 function setForm($form) {
98 $this->form = $form;
99 }
100
101 102 103
104 function validate(){
105 $this->errors = null;
106 $this->php($this->form->getData());
107
108 return $this->errors;
109 }
110
111 112 113 114 115 116 117
118 function validationError($fieldName, $message, $messageType='') {
119 $this->errors[] = array(
120 'fieldName' => $fieldName,
121 'message' => $message,
122 'messageType' => $messageType,
123 );
124 }
125
126 127 128
129 function showError() {
130 Debug::show($this->errors);
131 }
132
133 134 135
136 function getCombinedError(){
137 if($this->errors) {
138 foreach($this->errors as $error){
139 $ret['message'] .= $error['message']."<br />";
140 $ret['messageType'] .= $error['messageType']."<br />";
141 }
142
143 return $ret;
144 }
145 }
146
147 148 149
150 function getError(){
151 return $this->getErrors();
152 }
153
154 155 156 157 158 159 160 161 162 163 164
165 function getErrors() {
166 return $this->errors;
167 }
168
169 function requireField($fieldName, $data) {
170 if(!$data[$fieldName]) $this->validationError($fieldName, "$fieldName is required.", "required");
171 }
172
173 function includeJavascriptValidation() {
174 if($this->getJavascriptValidationHandler() == 'prototype') {
175 Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/prototype/prototype.js");
176 Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/behaviour/behaviour.js");
177 Requirements::javascript(SAPPHIRE_DIR . "/javascript/prototype_improvements.js");
178 Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
179 Requirements::javascript(SAPPHIRE_DIR . "/javascript/Validator.js");
180
181 $code = $this->javascript();
182 $formID = $this->form->FormName();
183 $js = <<<JS
184 Behaviour.register({
185 '#$formID': {
186 validate : function(fromAnOnBlur) {
187 initialiseForm(this, fromAnOnBlur);
188 $code
189
190 var error = hasHadFormError();
191 if(!error && fromAnOnBlur) clearErrorMessage(fromAnOnBlur.parentNode);
192 if(error && !fromAnOnBlur) focusOnFirstErroredField();
193
194 return !error;
195 },
196 onsubmit : function() {
197 if(typeof this.bypassValidation == 'undefined' || !this.bypassValidation) return this.validate();
198 }
199 },
200 '#$formID input' : {
201 initialise: function() {
202 if (this.type != 'submit') { // no blur for submit inputs
203 if(!this.old_onblur) this.old_onblur = function() { return true; }
204 if(!this.old_onfocus) this.old_onfocus = function() { return true; }
205 } else {
206 if(!this.old_onblur) this.old_onblur = function() { return false; }
207 if(!this.old_onfocus) this.old_onfocus = function() { return false; }
208 }
209 },
210 onblur : function() {
211 if(this.old_onblur()) {
212 // Don't perform instant validation for CalendarDateField fields; it creates usability wierdness.
213 if(this.parentNode.className.indexOf('calendardate') == -1 || this.value) {
214 return $('$formID').validate(this);
215 } else {
216 return true;
217 }
218 }
219 }
220 },
221 '#$formID textarea' : {
222 initialise: function() {
223 if(!this.old_onblur) this.old_onblur = function() { return true; }
224 if(!this.old_onfocus) this.old_onfocus = function() { return true; }
225 },
226 onblur : function() {
227 if(this.old_onblur()) {
228 return $('$formID').validate(this);
229 }
230 }
231 },
232 '#$formID select' : {
233 initialise: function() {
234 if(!this.old_onblur) this.old_onblur = function() { return true; }
235 },
236 onblur : function() {
237 if(this.old_onblur()) {
238 return $('$formID').validate(this);
239 }
240 }
241 }
242 });
243 JS;
244
245 Requirements::customScript($js);
246
247 if($this->form) $this->form->jsValidationIncluded = true;
248 }
249 }
250
251 252 253 254 255
256 function fieldIsRequired($fieldName) {
257 return false;
258 }
259
260 abstract function javascript();
261
262 abstract function php($data);
263 }
264 ?>
265
[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.
-