1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Extends RequiredFields so we can prevent DO writes in AW's controller(s) without needing to catch Exceptions from DO->validate() all over the place. |
4
|
|
|
* Note specifically $this->getExtendedValidationRoutines() - anti-pattern anyone? |
5
|
|
|
* |
6
|
|
|
* @author Russell Michell [email protected] |
7
|
|
|
* @package advancedworkflow |
8
|
|
|
*/ |
9
|
|
|
class AWRequiredFields extends RequiredFields { |
|
|
|
|
10
|
|
|
|
11
|
|
|
protected $data = array(); |
12
|
|
|
protected static $caller; |
13
|
|
|
|
14
|
|
|
public function php($data) { |
15
|
|
|
$valid = parent::php($data); |
16
|
|
|
$this->setData($data); |
17
|
|
|
|
18
|
|
|
// Fetch any extended validation routines on the caller |
19
|
|
|
$extended = $this->getExtendedValidationRoutines(); |
20
|
|
|
|
21
|
|
|
// Only deal-to extended routines once the parent is done |
22
|
|
|
if($valid && $extended['fieldValid'] !== true) { |
23
|
|
|
$fieldName = $extended['fieldName']; |
24
|
|
|
$formField = $extended['fieldField']; |
25
|
|
|
$errorMessage = sprintf($extended['fieldMsg'], |
26
|
|
|
strip_tags('"'.(($formField && $formField->Title()) ? $formField->Title() : $fieldName).'"')); |
27
|
|
|
|
28
|
|
|
if($formField && $msg = $formField->getCustomValidationMessage()) { |
29
|
|
|
$errorMessage = $msg; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->validationError( |
33
|
|
|
$fieldName, |
34
|
|
|
$errorMessage, |
35
|
|
|
"required" |
36
|
|
|
); |
37
|
|
|
$valid = false; |
38
|
|
|
} |
39
|
|
|
return $valid; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/* |
43
|
|
|
* Allows for the addition of an arbitrary no. additional, dedicated and "extended" validation methods on classes that call AWRequiredFields. |
44
|
|
|
* To add specific validation methods to a caller: |
45
|
|
|
* |
46
|
|
|
* 1). Write each checking method using this naming prototype: public function extendedRequiredFieldsXXX(). All methods so named will be called. |
47
|
|
|
* 2). Call AWRequiredFields->setCaller($this) |
48
|
|
|
* |
49
|
|
|
* Each extended method thus called, should return an array of a specific format. (See: static $extendedMethodReturn on the caller) |
50
|
|
|
* |
51
|
|
|
* @return array $return |
52
|
|
|
*/ |
53
|
|
|
public function getExtendedValidationRoutines() { |
54
|
|
|
// Setup a return array |
55
|
|
|
$return = array( |
56
|
|
|
'fieldValid'=>true, |
57
|
|
|
'fieldName' =>null, |
58
|
|
|
'fieldField'=>null, |
59
|
|
|
'fieldMsg' =>null |
60
|
|
|
); |
61
|
|
|
$caller = $this->getCaller(); |
62
|
|
|
$methods = get_class_methods($caller); |
63
|
|
|
if(!$methods) { |
|
|
|
|
64
|
|
|
return $return; |
65
|
|
|
} |
66
|
|
|
foreach($methods as $method) { |
67
|
|
|
if(!preg_match("#extendedRequiredFields#",$method)) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
// One of the DO's validation methods has failed |
71
|
|
|
$extended = $caller->$method($this->getData()); |
72
|
|
|
if($extended['fieldValid'] !== true) { |
73
|
|
|
$return['fieldValid'] = $extended['fieldValid']; |
74
|
|
|
$return['fieldName'] = $extended['fieldName']; |
75
|
|
|
$return['fieldField'] = $extended['fieldField']; |
76
|
|
|
$return['fieldMsg'] = $extended['fieldMsg']; |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return $return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function setData($data) { |
84
|
|
|
$this->data = $data; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getData() { |
88
|
|
|
return $this->data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setCaller($caller) { |
92
|
|
|
self::$caller = $caller; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getCaller() { |
96
|
|
|
return self::$caller; |
97
|
|
|
} |
98
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.