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
*/
@@ -102,6 +104,11 @@ public function __construct($name, $title = null, $value = "", $form = null, $sh
102
104
$ this ->setValue ($ value );
103
105
}
104
106
107
+ /**
108
+ * @param array $properties
109
+ *
110
+ * @return string
111
+ */
105
112
public function Field ($ properties = array ()) {
106
113
Requirements::javascript (FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js ' );
107
114
Requirements::javascript (FRAMEWORK_DIR . '/javascript/ConfirmedPasswordField.js ' );
@@ -146,65 +153,91 @@ public function Field($properties = array()) {
146
153
}
147
154
148
155
/**
149
- * Can be empty is a flag that turns on/off empty field checking.
156
+ * Can be empty is a flag that turns on / off empty field checking.
157
+ *
150
158
* For example, set this to false (the default) when creating a user account,
151
- * and true
159
+ * and true when displaying on an edit form.
160
+ *
161
+ * @param boolean $value
162
+ *
163
+ * @return ConfirmedPasswordField
152
164
*/
153
165
public function setCanBeEmpty ($ value ) {
154
166
$ this ->canBeEmpty = (bool )$ value ;
167
+
155
168
return $ this ;
156
169
}
157
170
158
171
/**
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.
172
+ * The title on the link which triggers display of the "password" and
173
+ * "confirm password" formfields. Only used if {@link setShowOnClick()}
174
+ * is set to TRUE.
162
175
*
163
- * @param $title
176
+ * @param string $title
177
+ *
178
+ * @return ConfirmedPasswordField
164
179
*/
165
180
public function setShowOnClickTitle ($ title ) {
166
181
$ this ->showOnClickTitle = $ title ;
182
+
167
183
return $ this ;
168
184
}
169
185
170
186
/**
171
- * @return string
187
+ * @return string $title
172
188
*/
173
189
public function getShowOnClickTitle () {
174
190
return $ this ->showOnClickTitle ;
175
191
}
176
192
193
+ /**
194
+ * @param string $title
195
+ *
196
+ * @return ConfirmedPasswordField
197
+ */
177
198
public function setRightTitle ($ title ) {
178
199
foreach ($ this ->children as $ field ) {
179
200
$ field ->setRightTitle ($ title );
180
201
}
202
+
181
203
return $ this ;
182
204
}
183
205
184
206
/**
185
- * @param array: 2 entrie array with the customised title for each of the 2 children.
207
+ * @param array $titles 2 entry array with the customized title for each
208
+ * of the 2 children.
209
+ *
210
+ * @return ConfirmedPasswordField
186
211
*/
187
212
public function setChildrenTitles ($ titles ) {
188
- if (is_array ($ titles )&& count ($ titles )== 2 ) {
213
+ if (is_array ($ titles ) && count ($ titles ) == 2 ) {
189
214
foreach ($ this ->children as $ field ) {
190
- if (isset ($ titles [0 ])){
215
+ if (isset ($ titles [0 ])) {
191
216
$ field ->setTitle ($ titles [0 ]);
217
+
192
218
array_shift ($ titles );
193
219
}
194
220
}
195
221
}
222
+
196
223
return $ this ;
197
224
}
198
225
199
226
/**
200
- * Value is sometimes an array, and sometimes a single value, so we need to handle both cases
227
+ * Value is sometimes an array, and sometimes a single value, so we need
228
+ * to handle both cases.
229
+ *
230
+ * @param mixed $value
231
+ *
232
+ * @return ConfirmedPasswordField
201
233
*/
202
234
public function setValue ($ value ) {
203
235
if (is_array ($ value )) {
204
236
if ($ value ['_Password ' ] || (!$ value ['_Password ' ] && !$ this ->canBeEmpty )) {
205
237
$ this ->value = $ value ['_Password ' ];
206
238
}
207
- if ($ this ->showOnClick && isset ($ value ['_PasswordFieldVisible ' ])){
239
+
240
+ if ($ this ->showOnClick && isset ($ value ['_PasswordFieldVisible ' ])) {
208
241
$ this ->children ->fieldByName ($ this ->getName () . '[_PasswordFieldVisible] ' )
209
242
->setValue ($ value ['_PasswordFieldVisible ' ]);
210
243
}
@@ -213,29 +246,40 @@ public function setValue($value) {
213
246
$ this ->value = $ value ;
214
247
}
215
248
}
216
- $ this ->children ->fieldByName ($ this ->getName () . '[_Password] ' )->setValue ($ this ->value );
217
- $ this ->children ->fieldByName ($ this ->getName () . '[_ConfirmPassword] ' )->setValue ($ this ->value );
249
+
250
+ $ this ->children ->fieldByName ($ this ->getName () . '[_Password] ' )
251
+ ->setValue ($ this ->value );
252
+
253
+ $ this ->children ->fieldByName ($ this ->getName () . '[_ConfirmPassword] ' )
254
+ ->setValue ($ this ->value );
218
255
219
256
return $ this ;
220
257
}
221
258
222
259
/**
223
- * Determines if the field was actually
224
- * shown on the clientside - if not,
260
+ * Determines if the field was actually shown on the client side - if not,
225
261
* we don't validate or save it.
226
262
*
227
- * @return bool
263
+ * @return boolean
228
264
*/
229
265
public function isSaveable () {
230
266
$ isVisible = $ this ->children ->fieldByName ($ this ->getName () . '[_PasswordFieldVisible] ' );
267
+
231
268
return (!$ this ->showOnClick || ($ this ->showOnClick && $ isVisible && $ isVisible ->Value ()));
232
269
}
233
270
271
+ /**
272
+ * @param Validator $validator
273
+ *
274
+ * @return boolean
275
+ */
234
276
public function validate ($ validator ) {
235
277
$ name = $ this ->name ;
236
278
237
279
// if field isn't visible, don't validate
238
- if (!$ this ->isSaveable ()) return true ;
280
+ if (!$ this ->isSaveable ()) {
281
+ return true ;
282
+ }
239
283
240
284
$ passwordField = $ this ->children ->fieldByName ($ name .'[_Password] ' );
241
285
$ passwordConfirmField = $ this ->children ->fieldByName ($ name .'[_ConfirmPassword] ' );
@@ -246,16 +290,26 @@ public function validate($validator) {
246
290
247
291
// both password-fields should be the same
248
292
if ($ value != $ passwordConfirmField ->Value ()) {
249
- $ validator ->validationError ($ name , _t ('Form.VALIDATIONPASSWORDSDONTMATCH ' ,"Passwords don't match " ),
250
- "validation " , false );
293
+ $ validator ->validationError (
294
+ $ name ,
295
+ _t ('Form.VALIDATIONPASSWORDSDONTMATCH ' ,"Passwords don't match " ),
296
+ "validation " ,
297
+ false
298
+ );
299
+
251
300
return false ;
252
301
}
253
302
254
303
if (!$ this ->canBeEmpty ) {
255
304
// both password-fields shouldn't be empty
256
305
if (!$ value || !$ passwordConfirmField ->Value ()) {
257
- $ validator ->validationError ($ name , _t ('Form.VALIDATIONPASSWORDSNOTEMPTY ' , "Passwords can't be empty " ),
258
- "validation " , false );
306
+ $ validator ->validationError (
307
+ $ name ,
308
+ _t ('Form.VALIDATIONPASSWORDSNOTEMPTY ' , "Passwords can't be empty " ),
309
+ "validation " ,
310
+ false
311
+ );
312
+
259
313
return false ;
260
314
}
261
315
}
@@ -302,29 +356,35 @@ public function validate($validator) {
302
356
"validation " ,
303
357
false
304
358
);
359
+
305
360
return false ;
306
361
}
307
362
}
363
+
308
364
return true ;
309
365
}
310
366
311
367
/**
312
- * Only save if field was shown on the client,
313
- * and is not empty.
368
+ * Only save if field was shown on the client, and is not empty.
369
+ *
370
+ * @param DataObjectInterface $record
314
371
*
315
- * @param DataObject $record
316
- * @return bool
372
+ * @return boolean
317
373
*/
318
374
public function saveInto (DataObjectInterface $ record ) {
319
- if (!$ this ->isSaveable ()) return false ;
375
+ if (!$ this ->isSaveable ()) {
376
+ return false ;
377
+ }
320
378
321
379
if (!($ this ->canBeEmpty && !$ this ->value )) {
322
380
parent ::saveInto ($ record );
323
381
}
324
382
}
325
383
326
384
/**
327
- * Makes a pretty readonly field with some stars in it
385
+ * Makes a read only field with some stars in it to replace the password
386
+ *
387
+ * @return ReadonlyField
328
388
*/
329
389
public function performReadonlyTransformation () {
330
390
$ field = $ this ->castedCopy ('ReadonlyField ' )
0 commit comments