2
2
3
3
namespace Codem \Utilities \HTML5 ;
4
4
5
- use Silverstripe \Forms \TextField ;
5
+ use Silverstripe \Forms \FormField ;
6
6
use SilverStripe \Forms \Validator ;
7
7
8
8
/**
9
9
* Provides a colour picker / field
10
+ *
10
11
* @see https://www.w3.org/wiki/Html/Elements/input/color
11
12
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color
12
13
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color#Value wrt value
14
+ * @see https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color) (default colour)
15
+ *
16
+ * Default value
17
+ * =============
18
+ * The colour input is special in that it does not allow an empty value within the current spec.
19
+ * The browser will always send through the default colour in the spec (#000000) if none is supplied
20
+ *
21
+ * In this implementation, the default field value set is static::WHITE
13
22
*/
14
- class ColorField extends TextField
23
+ class ColorField extends FormField
15
24
{
16
25
17
26
use Core;
18
27
use Datalist;
19
28
29
+ /**
30
+ * @var string
31
+ */
32
+ const WHITE = "#ffffff " ;
33
+
34
+ /**
35
+ * @var string
36
+ */
20
37
protected $ inputType = 'color ' ;
21
38
22
- protected $ defaultValue = "#ffffff " ;
39
+ /**
40
+ * This is the default colour for the input,
41
+ * if no value is set, or if the value provided is not a valid hex colour string
42
+ * On field construction this will be set to white
43
+ * @var string|null
44
+ */
45
+ protected $ defaultValue = null ;
46
+
47
+ /**
48
+ * Returns a colour input field
49
+ *
50
+ * @param string $name
51
+ * @param null|string $title
52
+ * @param string $value
53
+ * @param string $defaultValue the default value to use for the input
54
+ *
55
+ */
56
+ public function __construct ($ name , $ title = null , $ value = '' , $ defaultValue = '' )
57
+ {
58
+ $ this ->setDefaultValue ($ defaultValue );
59
+ parent ::__construct ($ name , $ title , $ value );
60
+ }
23
61
24
62
/**
25
63
* Returns the value saved as a 6 chr RGB colour with # prefixed
@@ -42,18 +80,39 @@ public function Value()
42
80
/**
43
81
* When the value is set, handle incorrect values
44
82
* @param string $value an RGB colour value as a 'valid simple colour'
45
- * @return void
46
83
*/
47
84
public function setValue ($ value , $ data = null )
48
85
{
49
86
$ this ->value = $ this ->getValidRGB ($ value );
87
+ return $ this ;
88
+ }
89
+
90
+
91
+ /**
92
+ * Set the default value to be used if an invalid colour is detected
93
+ * The default is static::WHITE
94
+ *
95
+ * @param string $defaultValue an RGB colour value as a 'valid simple colour'
96
+ */
97
+ public function setDefaultValue (string $ defaultValue ) : self
98
+ {
99
+ $ this ->defaultValue = $ this ->getValidRGB ($ defaultValue );
100
+ return $ this ;
101
+ }
102
+
103
+ /**
104
+ * Get the current default value
105
+ */
106
+ public function getDefaultValue () : string
107
+ {
108
+ return $ this ->defaultValue ;
50
109
}
51
110
52
111
/**
53
112
* Base on the value return either the defaultValue colour value or the value
54
113
* @return string
55
114
* @param string $value the value to check
56
- * @param Validator $validator optional, if set relevant errors will be added
115
+ * @param Validator $validator optional, see isValidRGB
57
116
*
58
117
* The only valid value here is defined by the "valid simple colour" definition at
59
118
* https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-simple-colour
@@ -64,15 +123,34 @@ public function setValue($value, $data = null)
64
123
* representing the red component, the middle two digits representing the green component,
65
124
* and the last two digits representing the blue component, in hexadecimal.</blockquote>
66
125
*/
67
- public function getValidRGB ($ value , Validator $ validator = null )
126
+ public function getValidRGB (? string $ value , Validator $ validator = null ) : string
68
127
{
69
128
70
- // empty values default to the empty value value
129
+ $ simpleColourValue = $ this ->defaultValue ?? static ::WHITE ;
130
+
71
131
if (!$ value ) {
72
- return $ this ->defaultValue ;
132
+ // no value provided .. return the defaultValue if set or WHITE
133
+ return $ simpleColourValue ;
73
134
}
74
135
75
136
$ value = strtolower ($ value );
137
+ if (!$ this ->isValidRGB ($ value , $ validator )) {
138
+ $ value = $ simpleColourValue ;
139
+ }
140
+
141
+ // Let result be a simple color.
142
+ return $ value ;
143
+ }
144
+
145
+ /**
146
+ * Check if the value provided is a valid RGB value
147
+ * @param string $value
148
+ * @param Validator $validator an optional validator. If provided specific errors will be stored in the validator
149
+ */
150
+ public function isValidRGB (?string $ value , Validator $ validator = null ) : bool {
151
+
152
+ // Ensure a string value
153
+ $ value = $ value ?? '' ;
76
154
77
155
// If input is not exactly seven characters long, then return an error.
78
156
if (mb_strlen ($ value ) != 7 ) {
@@ -86,7 +164,7 @@ public function getValidRGB($value, Validator $validator = null)
86
164
"validation "
87
165
);
88
166
}
89
- return $ this -> defaultValue ;
167
+ return false ;
90
168
}
91
169
92
170
// If the first character in input is not a U+0023 NUMBER SIGN character (#), then return an error.
@@ -101,7 +179,7 @@ public function getValidRGB($value, Validator $validator = null)
101
179
"validation "
102
180
);
103
181
}
104
- return $ this -> defaultValue ;
182
+ return false ;
105
183
}
106
184
107
185
// If the last six characters of input are not all ASCII hex digits, then return an error.
@@ -117,11 +195,11 @@ public function getValidRGB($value, Validator $validator = null)
117
195
"validation "
118
196
);
119
197
}
120
- return $ this -> defaultValue ;
198
+ return false ;
121
199
}
122
200
123
- // Let result be a simple color.
124
- return $ value ;
201
+ // all tests pass
202
+ return true ;
125
203
}
126
204
127
205
/**
@@ -132,10 +210,7 @@ public function getValidRGB($value, Validator $validator = null)
132
210
*/
133
211
public function validate ($ validator )
134
212
{
135
- if (!$ this ->getValidRGB ($ this ->value , $ validator )) {
136
- return false ;
137
- }
138
- return true ;
213
+ return $ this ->isValidRGB ($ this ->Value (), $ validator );
139
214
}
140
215
141
216
}
0 commit comments