1
1
<?php
2
+
2
3
/**
3
4
* Two masked input fields, checks for matching passwords.
4
- * Optionally hides the fields by default and shows
5
- * a link to toggle their visibility.
5
+ *
6
+ * Optionally hides the fields by default and shows a link to toggle their
7
+ * visibility.
6
8
*
7
9
* @package forms
8
10
* @subpackage fields-formattedinput
@@ -39,11 +41,12 @@ class ConfirmedPasswordField extends FormField {
39
41
public $ canBeEmpty = false ;
40
42
41
43
/**
42
- * If set to TRUE, the "password" and "confirm password"
43
- * formfields will be hidden via CSS and JavaScript by default,
44
- * and triggered by a link. An additional hidden field
45
- * determines if showing the fields has been triggered,
46
- * and just validates/saves the input in this case.
44
+ * If set to TRUE, the "password" and "confirm password" form fields will
45
+ * be hidden via CSS and JavaScript by default, and triggered by a link.
46
+ *
47
+ * An additional hidden field determines if showing the fields has been
48
+ * triggered and just validates/saves the input in this case.
49
+ *
47
50
* This behaviour works unobtrusively, without JavaScript enabled
48
51
* the fields show, validate and save by default.
49
52
*
@@ -52,8 +55,7 @@ class ConfirmedPasswordField extends FormField {
52
55
protected $ showOnClick = false ;
53
56
54
57
/**
55
- * Title for the link that triggers
56
- * the visibility of password fields.
58
+ * Title for the link that triggers the visibility of password fields.
57
59
*
58
60
* @var string
59
61
*/
@@ -93,6 +95,12 @@ public function __construct($name, $title = null, $value = "", $form = null, $sh
93
95
if ($ showOnClick ) {
94
96
$ this ->children ->push (new HiddenField ("{$ name }[_PasswordFieldVisible] " ));
95
97
}
98
+
99
+ // disable auto complete
100
+ foreach ($ this ->children as $ child ) {
101
+ $ child ->setAttribute ('autocomplete ' , 'off ' );
102
+ }
103
+
96
104
$ this ->showOnClick = $ showOnClick ;
97
105
98
106
// we have labels for the subfields
@@ -102,6 +110,11 @@ public function __construct($name, $title = null, $value = "", $form = null, $sh
102
110
$ this ->setValue ($ value );
103
111
}
104
112
113
+ /**
114
+ * @param array $properties
115
+ *
116
+ * @return string
117
+ */
105
118
public function Field ($ properties = array ()) {
106
119
Requirements::javascript (FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js ' );
107
120
Requirements::javascript (FRAMEWORK_DIR . '/javascript/ConfirmedPasswordField.js ' );
@@ -129,11 +142,13 @@ public function Field($properties = array()) {
129
142
foreach ($ this ->children as $ field ) {
130
143
$ field ->setDisabled ($ this ->isDisabled ());
131
144
$ field ->setReadonly ($ this ->isReadonly ());
145
+
132
146
if (count ($ this ->attributes )) {
133
147
foreach ($ this ->attributes as $ name => $ value ) {
134
148
$ field ->setAttribute ($ name , $ value );
135
149
}
136
150
}
151
+
137
152
$ content .= $ field ->FieldHolder ();
138
153
}
139
154
@@ -146,65 +161,91 @@ public function Field($properties = array()) {
146
161
}
147
162
148
163
/**
149
- * Can be empty is a flag that turns on/off empty field checking.
164
+ * Can be empty is a flag that turns on / off empty field checking.
165
+ *
150
166
* For example, set this to false (the default) when creating a user account,
151
- * and true
167
+ * and true when displaying on an edit form.
168
+ *
169
+ * @param boolean $value
170
+ *
171
+ * @return ConfirmedPasswordField
152
172
*/
153
173
public function setCanBeEmpty ($ value ) {
154
174
$ this ->canBeEmpty = (bool )$ value ;
175
+
155
176
return $ this ;
156
177
}
157
178
158
179
/**
159
- * The title on the link which triggers display of the
160
- * "password" and " confirm password" formfields.
161
- * Only used if {@link setShowOnClick()} is set to TRUE.
180
+ * The title on the link which triggers display of the "password" and
181
+ * "confirm password" formfields. Only used if {@link setShowOnClick()}
182
+ * is set to TRUE.
162
183
*
163
- * @param $title
184
+ * @param string $title
185
+ *
186
+ * @return ConfirmedPasswordField
164
187
*/
165
188
public function setShowOnClickTitle ($ title ) {
166
189
$ this ->showOnClickTitle = $ title ;
190
+
167
191
return $ this ;
168
192
}
169
193
170
194
/**
171
- * @return string
195
+ * @return string $title
172
196
*/
173
197
public function getShowOnClickTitle () {
174
198
return $ this ->showOnClickTitle ;
175
199
}
176
200
201
+ /**
202
+ * @param string $title
203
+ *
204
+ * @return ConfirmedPasswordField
205
+ */
177
206
public function setRightTitle ($ title ) {
178
207
foreach ($ this ->children as $ field ) {
179
208
$ field ->setRightTitle ($ title );
180
209
}
210
+
181
211
return $ this ;
182
212
}
183
213
184
214
/**
185
- * @param array: 2 entrie array with the customised title for each of the 2 children.
215
+ * @param array $titles 2 entry array with the customized title for each
216
+ * of the 2 children.
217
+ *
218
+ * @return ConfirmedPasswordField
186
219
*/
187
220
public function setChildrenTitles ($ titles ) {
188
- if (is_array ($ titles )&& count ($ titles )== 2 ) {
221
+ if (is_array ($ titles ) && count ($ titles ) == 2 ) {
189
222
foreach ($ this ->children as $ field ) {
190
- if (isset ($ titles [0 ])){
223
+ if (isset ($ titles [0 ])) {
191
224
$ field ->setTitle ($ titles [0 ]);
225
+
192
226
array_shift ($ titles );
193
227
}
194
228
}
195
229
}
230
+
196
231
return $ this ;
197
232
}
198
233
199
234
/**
200
- * Value is sometimes an array, and sometimes a single value, so we need to handle both cases
235
+ * Value is sometimes an array, and sometimes a single value, so we need
236
+ * to handle both cases.
237
+ *
238
+ * @param mixed $value
239
+ *
240
+ * @return ConfirmedPasswordField
201
241
*/
202
242
public function setValue ($ value ) {
203
243
if (is_array ($ value )) {
204
244
if ($ value ['_Password ' ] || (!$ value ['_Password ' ] && !$ this ->canBeEmpty )) {
205
245
$ this ->value = $ value ['_Password ' ];
206
246
}
207
- if ($ this ->showOnClick && isset ($ value ['_PasswordFieldVisible ' ])){
247
+
248
+ if ($ this ->showOnClick && isset ($ value ['_PasswordFieldVisible ' ])) {
208
249
$ this ->children ->fieldByName ($ this ->getName () . '[_PasswordFieldVisible] ' )
209
250
->setValue ($ value ['_PasswordFieldVisible ' ]);
210
251
}
@@ -213,29 +254,40 @@ public function setValue($value) {
213
254
$ this ->value = $ value ;
214
255
}
215
256
}
216
- $ this ->children ->fieldByName ($ this ->getName () . '[_Password] ' )->setValue ($ this ->value );
217
- $ this ->children ->fieldByName ($ this ->getName () . '[_ConfirmPassword] ' )->setValue ($ this ->value );
257
+
258
+ $ this ->children ->fieldByName ($ this ->getName () . '[_Password] ' )
259
+ ->setValue ($ this ->value );
260
+
261
+ $ this ->children ->fieldByName ($ this ->getName () . '[_ConfirmPassword] ' )
262
+ ->setValue ($ this ->value );
218
263
219
264
return $ this ;
220
265
}
221
266
222
267
/**
223
- * Determines if the field was actually
224
- * shown on the clientside - if not,
268
+ * Determines if the field was actually shown on the client side - if not,
225
269
* we don't validate or save it.
226
270
*
227
- * @return bool
271
+ * @return boolean
228
272
*/
229
273
public function isSaveable () {
230
274
$ isVisible = $ this ->children ->fieldByName ($ this ->getName () . '[_PasswordFieldVisible] ' );
275
+
231
276
return (!$ this ->showOnClick || ($ this ->showOnClick && $ isVisible && $ isVisible ->Value ()));
232
277
}
233
278
279
+ /**
280
+ * @param Validator $validator
281
+ *
282
+ * @return boolean
283
+ */
234
284
public function validate ($ validator ) {
235
285
$ name = $ this ->name ;
236
286
237
287
// if field isn't visible, don't validate
238
- if (!$ this ->isSaveable ()) return true ;
288
+ if (!$ this ->isSaveable ()) {
289
+ return true ;
290
+ }
239
291
240
292
$ passwordField = $ this ->children ->fieldByName ($ name .'[_Password] ' );
241
293
$ passwordConfirmField = $ this ->children ->fieldByName ($ name .'[_ConfirmPassword] ' );
@@ -246,16 +298,26 @@ public function validate($validator) {
246
298
247
299
// both password-fields should be the same
248
300
if ($ value != $ passwordConfirmField ->Value ()) {
249
- $ validator ->validationError ($ name , _t ('Form.VALIDATIONPASSWORDSDONTMATCH ' ,"Passwords don't match " ),
250
- "validation " , false );
301
+ $ validator ->validationError (
302
+ $ name ,
303
+ _t ('Form.VALIDATIONPASSWORDSDONTMATCH ' ,"Passwords don't match " ),
304
+ "validation " ,
305
+ false
306
+ );
307
+
251
308
return false ;
252
309
}
253
310
254
311
if (!$ this ->canBeEmpty ) {
255
312
// both password-fields shouldn't be empty
256
313
if (!$ value || !$ passwordConfirmField ->Value ()) {
257
- $ validator ->validationError ($ name , _t ('Form.VALIDATIONPASSWORDSNOTEMPTY ' , "Passwords can't be empty " ),
258
- "validation " , false );
314
+ $ validator ->validationError (
315
+ $ name ,
316
+ _t ('Form.VALIDATIONPASSWORDSNOTEMPTY ' , "Passwords can't be empty " ),
317
+ "validation " ,
318
+ false
319
+ );
320
+
259
321
return false ;
260
322
}
261
323
}
@@ -302,29 +364,35 @@ public function validate($validator) {
302
364
"validation " ,
303
365
false
304
366
);
367
+
305
368
return false ;
306
369
}
307
370
}
371
+
308
372
return true ;
309
373
}
310
374
311
375
/**
312
- * Only save if field was shown on the client,
313
- * and is not empty.
376
+ * Only save if field was shown on the client, and is not empty.
377
+ *
378
+ * @param DataObjectInterface $record
314
379
*
315
- * @param DataObject $record
316
- * @return bool
380
+ * @return boolean
317
381
*/
318
382
public function saveInto (DataObjectInterface $ record ) {
319
- if (!$ this ->isSaveable ()) return false ;
383
+ if (!$ this ->isSaveable ()) {
384
+ return false ;
385
+ }
320
386
321
387
if (!($ this ->canBeEmpty && !$ this ->value )) {
322
388
parent ::saveInto ($ record );
323
389
}
324
390
}
325
391
326
392
/**
327
- * Makes a pretty readonly field with some stars in it
393
+ * Makes a read only field with some stars in it to replace the password
394
+ *
395
+ * @return ReadonlyField
328
396
*/
329
397
public function performReadonlyTransformation () {
330
398
$ field = $ this ->castedCopy ('ReadonlyField ' )
0 commit comments