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