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