Skip to content

Commit 09fcdb8

Browse files
committed
Coding conventions for ConfirmedPasswordField
1 parent d47b202 commit 09fcdb8

File tree

1 file changed

+97
-37
lines changed

1 file changed

+97
-37
lines changed

forms/ConfirmedPasswordField.php

+97-37
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
/**
34
* 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.
68
*
79
* @package forms
810
* @subpackage fields-formattedinput
@@ -39,11 +41,12 @@ class ConfirmedPasswordField extends FormField {
3941
public $canBeEmpty = false;
4042

4143
/**
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+
*
4750
* This behaviour works unobtrusively, without JavaScript enabled
4851
* the fields show, validate and save by default.
4952
*
@@ -52,8 +55,7 @@ class ConfirmedPasswordField extends FormField {
5255
protected $showOnClick = false;
5356

5457
/**
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.
5759
*
5860
* @var string
5961
*/
@@ -102,6 +104,11 @@ public function __construct($name, $title = null, $value = "", $form = null, $sh
102104
$this->setValue($value);
103105
}
104106

107+
/**
108+
* @param array $properties
109+
*
110+
* @return string
111+
*/
105112
public function Field($properties = array()) {
106113
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
107114
Requirements::javascript(FRAMEWORK_DIR . '/javascript/ConfirmedPasswordField.js');
@@ -146,65 +153,91 @@ public function Field($properties = array()) {
146153
}
147154

148155
/**
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+
*
150158
* 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
152164
*/
153165
public function setCanBeEmpty($value) {
154166
$this->canBeEmpty = (bool)$value;
167+
155168
return $this;
156169
}
157170

158171
/**
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.
162175
*
163-
* @param $title
176+
* @param string $title
177+
*
178+
* @return ConfirmedPasswordField
164179
*/
165180
public function setShowOnClickTitle($title) {
166181
$this->showOnClickTitle = $title;
182+
167183
return $this;
168184
}
169185

170186
/**
171-
* @return string
187+
* @return string $title
172188
*/
173189
public function getShowOnClickTitle() {
174190
return $this->showOnClickTitle;
175191
}
176192

193+
/**
194+
* @param string $title
195+
*
196+
* @return ConfirmedPasswordField
197+
*/
177198
public function setRightTitle($title) {
178199
foreach($this->children as $field) {
179200
$field->setRightTitle($title);
180201
}
202+
181203
return $this;
182204
}
183205

184206
/**
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
186211
*/
187212
public function setChildrenTitles($titles) {
188-
if(is_array($titles)&&count($titles)==2){
213+
if(is_array($titles) && count($titles) == 2) {
189214
foreach($this->children as $field) {
190-
if(isset($titles[0])){
215+
if(isset($titles[0])) {
191216
$field->setTitle($titles[0]);
217+
192218
array_shift($titles);
193219
}
194220
}
195221
}
222+
196223
return $this;
197224
}
198225

199226
/**
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
201233
*/
202234
public function setValue($value) {
203235
if(is_array($value)) {
204236
if($value['_Password'] || (!$value['_Password'] && !$this->canBeEmpty)) {
205237
$this->value = $value['_Password'];
206238
}
207-
if($this->showOnClick && isset($value['_PasswordFieldVisible'])){
239+
240+
if($this->showOnClick && isset($value['_PasswordFieldVisible'])) {
208241
$this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]')
209242
->setValue($value['_PasswordFieldVisible']);
210243
}
@@ -213,29 +246,40 @@ public function setValue($value) {
213246
$this->value = $value;
214247
}
215248
}
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);
218255

219256
return $this;
220257
}
221258

222259
/**
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,
225261
* we don't validate or save it.
226262
*
227-
* @return bool
263+
* @return boolean
228264
*/
229265
public function isSaveable() {
230266
$isVisible = $this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]');
267+
231268
return (!$this->showOnClick || ($this->showOnClick && $isVisible && $isVisible->Value()));
232269
}
233270

271+
/**
272+
* @param Validator $validator
273+
*
274+
* @return boolean
275+
*/
234276
public function validate($validator) {
235277
$name = $this->name;
236278

237279
// if field isn't visible, don't validate
238-
if(!$this->isSaveable()) return true;
280+
if(!$this->isSaveable()) {
281+
return true;
282+
}
239283

240284
$passwordField = $this->children->fieldByName($name.'[_Password]');
241285
$passwordConfirmField = $this->children->fieldByName($name.'[_ConfirmPassword]');
@@ -246,16 +290,26 @@ public function validate($validator) {
246290

247291
// both password-fields should be the same
248292
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+
251300
return false;
252301
}
253302

254303
if(!$this->canBeEmpty) {
255304
// both password-fields shouldn't be empty
256305
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+
259313
return false;
260314
}
261315
}
@@ -302,29 +356,35 @@ public function validate($validator) {
302356
"validation",
303357
false
304358
);
359+
305360
return false;
306361
}
307362
}
363+
308364
return true;
309365
}
310366

311367
/**
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
314371
*
315-
* @param DataObject $record
316-
* @return bool
372+
* @return boolean
317373
*/
318374
public function saveInto(DataObjectInterface $record) {
319-
if(!$this->isSaveable()) return false;
375+
if(!$this->isSaveable()) {
376+
return false;
377+
}
320378

321379
if(!($this->canBeEmpty && !$this->value)) {
322380
parent::saveInto($record);
323381
}
324382
}
325383

326384
/**
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
328388
*/
329389
public function performReadonlyTransformation() {
330390
$field = $this->castedCopy('ReadonlyField')

0 commit comments

Comments
 (0)