2007-09-11 02:26:55 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Shows two password-fields, and checks for matching passwords.
|
2007-12-19 02:45:22 +01:00
|
|
|
* Optionally hides the fields by default and shows
|
|
|
|
* a link to toggle their visibility.
|
2008-04-05 01:15:42 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-09-11 02:26:55 +02:00
|
|
|
*/
|
|
|
|
class ConfirmedPasswordField extends FormField {
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Minimum character length of the password.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-04-26 08:35:50 +02:00
|
|
|
public $minLength = null;
|
2007-09-11 02:26:55 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Maximum character length of the password.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-04-26 08:35:50 +02:00
|
|
|
public $maxLength = null;
|
2007-09-11 02:26:55 +02:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Enforces at least one digit and one alphanumeric
|
|
|
|
* character (in addition to {$minLength} and {$maxLength}
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2007-09-11 02:26:55 +02:00
|
|
|
public $requireStrongPassword = false;
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Allow empty fields in serverside validation
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2007-09-11 02:26:55 +02:00
|
|
|
public $canBeEmpty = false;
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Hide the two password fields by default,
|
|
|
|
* and provide a link instead that toggles them.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $showOnClick = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Title for the link that triggers
|
|
|
|
* the visibility of password fields.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $showOnClickTitle = 'Change Password';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $title
|
|
|
|
* @param mixed $value
|
|
|
|
* @param Form $form
|
|
|
|
* @param boolean $showOnClick
|
2008-04-09 13:29:29 +02:00
|
|
|
* @param string $titleConfirmField Alternate title (not localizeable)
|
2007-12-19 02:45:22 +01:00
|
|
|
*/
|
2008-04-09 13:29:29 +02:00
|
|
|
function __construct($name, $title = null, $value = "", $form = null, $showOnClick = false, $titleConfirmField = null) {
|
2007-09-11 02:26:55 +02:00
|
|
|
// naming with underscores to prevent values from actually being saved somewhere
|
|
|
|
$this->children = new FieldSet(
|
2008-04-09 13:29:29 +02:00
|
|
|
new PasswordField(
|
|
|
|
"{$name}[_Password]",
|
|
|
|
(isset($title)) ? $title : _t('Member.PASSWORD')),
|
|
|
|
new PasswordField(
|
|
|
|
"{$name}[_ConfirmPassword]",
|
|
|
|
(isset($titleConfirmField)) ? $titleConfirmField : _t('Member.CONFIRMPASSWORD', 'Confirm Password')
|
|
|
|
)
|
2007-09-11 02:26:55 +02:00
|
|
|
);
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
$this->showOnClick = $showOnClick;
|
|
|
|
if($this->showOnClick) {
|
|
|
|
$this->children->push(new HiddenField("{$name}[_PasswordFieldVisible]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have labels for the subfields
|
|
|
|
$title = false;
|
2007-09-11 02:26:55 +02:00
|
|
|
|
|
|
|
parent::__construct($name, $title, $value, $form);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Field() {
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/prototype.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/behaviour.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/prototype_improvements.js');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/javascript/ConfirmedPasswordField.js');
|
2007-12-19 02:45:22 +01:00
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
$content = '';
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
if($this->showOnClick) {
|
|
|
|
|
|
|
|
$content .= "<div class=\"showOnClick\">\n";
|
2008-04-06 06:08:45 +02:00
|
|
|
$content .= "<a href=\"#\"" . $this->getTabIndexHTML() . ">{$this->showOnClickTitle}</a>\n";
|
2007-12-19 02:45:22 +01:00
|
|
|
$content .= "<div class=\"showOnClickContainer\">";
|
|
|
|
}
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
foreach($this->children as $field) {
|
2007-12-19 02:45:22 +01:00
|
|
|
$content .= $field->FieldHolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->showOnClick) {
|
|
|
|
$content .= "</div>\n";
|
|
|
|
$content .= "</div>\n";
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be empty is a flag that turns on/off empty field checking.
|
|
|
|
* For example, set this to false (the default) when creating a user account,
|
|
|
|
* and true
|
|
|
|
*/
|
|
|
|
function setCanBeEmpty($value) {
|
|
|
|
$this->canBeEmpty = (bool)$value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setRightTitle($title) {
|
|
|
|
foreach($this->children as $field) {
|
|
|
|
$field->setRightTitle($title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Value is sometimes an array, and sometimes a single value, so we need to handle both cases
|
|
|
|
*/
|
|
|
|
function setValue($value) {
|
|
|
|
if(is_array($value)) {
|
|
|
|
if($value['_Password'] || (!$value['_Password'] && !$this->canBeEmpty)) {
|
|
|
|
$this->value = $value['_Password'];
|
|
|
|
}
|
2008-05-24 03:22:27 +02:00
|
|
|
if(isset($value['_PasswordFieldVisible'])){
|
|
|
|
$this->children->fieldByName($this->Name() . '[_PasswordFieldVisible]')->setValue($value['_PasswordFieldVisible']);
|
|
|
|
}
|
2007-09-11 02:26:55 +02:00
|
|
|
} else {
|
|
|
|
if($value || (!$value && !$this->canBeEmpty)) {
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
function jsValidation() {
|
2007-09-11 02:26:55 +02:00
|
|
|
$formID = $this->form->FormName();
|
2007-12-19 02:45:22 +01:00
|
|
|
$jsTests = '';
|
|
|
|
|
|
|
|
$jsTests .= "
|
|
|
|
// if fields are hidden, reset values and don't validate
|
|
|
|
var containers = $$('.showOnClickContainer', $('#'+fieldName));
|
|
|
|
if(containers.length && !Element.visible(containers[0])) {
|
|
|
|
passEl.value = null;
|
|
|
|
confEl.value = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
";
|
2008-01-10 04:28:13 +01:00
|
|
|
|
|
|
|
$error1 = _t('ConfirmedPasswordField.HAVETOMATCH', 'Passwords have to match.');
|
2007-12-19 02:45:22 +01:00
|
|
|
$jsTests .= "
|
2007-09-11 02:26:55 +02:00
|
|
|
if(passEl.value != confEl.value) {
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(confEl, \"$error1\", \"error\");
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
2008-01-10 04:28:13 +01:00
|
|
|
$error2 = _t('ConfirmedPasswordField.NOEMPTY', 'Passwords can\'t be empty.');
|
2007-09-11 02:26:55 +02:00
|
|
|
if(!$this->canBeEmpty) {
|
|
|
|
$jsTests .= "
|
|
|
|
if(!passEl.value || !confEl.value) {
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(confEl, \"$error2\", \"error\");
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
";
|
|
|
|
}
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
if(($this->minLength || $this->maxLength)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
if($this->minLength && $this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength},{$this->maxLength}}";
|
2008-01-10 04:28:13 +01:00
|
|
|
$errorMsg = sprintf(_t('ConfirmedPasswordField.BETWEEN', 'Passwords must be %s to %s characters long.'), $this->minLength, $this->maxLength);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->minLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength}}.*";
|
2008-01-10 04:28:13 +01:00
|
|
|
$errorMsg = sprintf(_t('ConfirmedPasswordField.ATLEAST', 'Passwords must be at least %s characters long.'), $this->minLength);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{0,{$this->maxLength}}";
|
2008-01-10 04:28:13 +01:00
|
|
|
$errorMsg = sprintf(_t('ConfirmedPasswordField.MAXIMUM', 'Passwords must be at most %s characters long.'), $this->maxLength);
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2008-09-12 07:40:55 +02:00
|
|
|
$limitRegex = '/^.' . $limit . '$/';
|
2007-09-11 02:26:55 +02:00
|
|
|
$jsTests .= "
|
2007-12-19 02:45:22 +01:00
|
|
|
if(passEl.value && !passEl.value.match({$limitRegex})) {
|
2007-09-11 02:26:55 +02:00
|
|
|
validationError(confEl, \"{$errorMsg}\", \"error\");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
";
|
|
|
|
}
|
|
|
|
|
2008-01-10 04:28:13 +01:00
|
|
|
$error3 = _t('ConfirmedPasswordField.LEASTONE', 'Passwords must have at least one digit and one alphanumeric character.');
|
2007-09-11 02:26:55 +02:00
|
|
|
if($this->requireStrongPassword) {
|
|
|
|
$jsTests .= "
|
|
|
|
if(!passEl.value.match(/^(([a-zA-Z]+\d+)|(\d+[a-zA-Z]+))[a-zA-Z0-9]*$/)) {
|
|
|
|
validationError(
|
|
|
|
confEl,
|
2008-01-10 04:28:13 +01:00
|
|
|
\"$error3\",
|
2007-09-11 02:26:55 +02:00
|
|
|
\"error\"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
";
|
|
|
|
}
|
|
|
|
|
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateConfirmedPassword: function(fieldName) {
|
|
|
|
var passEl = _CURRENT_FORM.elements['Password[_Password]'];
|
|
|
|
var confEl = _CURRENT_FORM.elements['Password[_ConfirmPassword]'];
|
|
|
|
$jsTests
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
Requirements :: customScript($jsFunc, 'func_validateConfirmedPassword');
|
|
|
|
|
|
|
|
//return "\$('$formID').validateConfirmedPassword('$this->name');";
|
|
|
|
return <<<JS
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
|
|
if(fromAnOnBlur.name == '$this->name')
|
|
|
|
$('$formID').validateConfirmedPassword('$this->name');
|
|
|
|
}else{
|
|
|
|
$('$formID').validateConfirmedPassword('$this->name');
|
|
|
|
}
|
|
|
|
JS;
|
|
|
|
}
|
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Determines if the field was actually
|
|
|
|
* shown on the clientside - if not,
|
|
|
|
* we don't validate or save it.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function isSaveable() {
|
|
|
|
$isVisible = $this->children->fieldByName($this->Name() . '[_PasswordFieldVisible]');
|
|
|
|
return (!$this->showOnClick || ($this->showOnClick && $isVisible && $isVisible->Value()));
|
|
|
|
}
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
function validate() {
|
|
|
|
$validator = $this->form->getValidator();
|
|
|
|
$name = $this->name;
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
// if field isn't visible, don't validate
|
|
|
|
if(!$this->isSaveable()) return true;
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
$passwordField = $this->children->fieldByName($name.'[_Password]');
|
|
|
|
$passwordConfirmField = $this->children->fieldByName($name.'[_ConfirmPassword]');
|
|
|
|
$passwordField->setValue($_POST[$name]['_Password']);
|
|
|
|
$passwordConfirmField->setValue($_POST[$name]['_ConfirmPassword']);
|
2007-12-19 02:45:22 +01:00
|
|
|
|
|
|
|
$value = $passwordField->Value();
|
|
|
|
|
2007-09-11 02:26:55 +02:00
|
|
|
// both password-fields should be the same
|
2007-12-19 02:45:22 +01:00
|
|
|
if($value != $passwordConfirmField->Value()) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError($name, _t('Form.VALIDATIONPASSWORDSDONTMATCH',"Passwords don't match"), "validation", false);
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->canBeEmpty) {
|
|
|
|
// both password-fields shouldn't be empty
|
2007-12-19 02:52:11 +01:00
|
|
|
if(!$value || !$passwordConfirmField->Value()) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError($name, _t('Form.VALIDATIONPASSWORDSNOTEMPTY', "Passwords can't be empty"), "validation", false);
|
2007-09-11 02:26:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lengths
|
2007-12-19 02:45:22 +01:00
|
|
|
if(($this->minLength || $this->maxLength)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
if($this->minLength && $this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength},{$this->maxLength}}";
|
2008-01-10 04:28:13 +01:00
|
|
|
$errorMsg = sprintf(_t('ConfirmedPasswordField.BETWEEN', 'Passwords must be %s to %s characters long.'), $this->minLength, $this->maxLength);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->minLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{{$this->minLength}}.*";
|
2008-01-10 04:28:13 +01:00
|
|
|
$errorMsg = sprintf(_t('ConfirmedPasswordField.ATLEAST', 'Passwords must be at least %s characters long.'), $this->minLength);
|
2007-09-11 02:26:55 +02:00
|
|
|
} elseif($this->maxLength) {
|
2008-09-12 07:40:55 +02:00
|
|
|
$limit = "{0,{$this->maxLength}}";
|
2008-01-10 04:28:13 +01:00
|
|
|
$errorMsg = sprintf(_t('ConfirmedPasswordField.MAXIMUM', 'Passwords must be at most %s characters long.'), $this->maxLength);
|
2007-09-11 02:26:55 +02:00
|
|
|
}
|
2008-09-12 07:40:55 +02:00
|
|
|
$limitRegex = '/^.' . $limit . '$/';
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!empty($value) && !preg_match($limitRegex,$value)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$validator->validationError('Password', $errorMsg,
|
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->requireStrongPassword) {
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!preg_match('/^(([a-zA-Z]+\d+)|(\d+[a-zA-Z]+))[a-zA-Z0-9]*$/',$value)) {
|
2007-09-11 02:26:55 +02:00
|
|
|
$validator->validationError(
|
|
|
|
'Password',
|
2007-10-25 04:47:45 +02:00
|
|
|
_t('Form.VALIDATIONSTRONGPASSWORD', "Passwords must have at least one digit and one alphanumeric character."),
|
2007-09-11 02:26:55 +02:00
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-11-12 03:58:27 +01:00
|
|
|
|
2007-12-19 02:45:22 +01:00
|
|
|
/**
|
|
|
|
* Only save if field was shown on the client,
|
|
|
|
* and is not empty.
|
|
|
|
*
|
|
|
|
* @param DataObject $record
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-11-12 03:58:27 +01:00
|
|
|
function saveInto(DataObject $record) {
|
2007-12-19 02:45:22 +01:00
|
|
|
if(!$this->isSaveable()) return false;
|
|
|
|
|
2007-11-12 03:58:27 +01:00
|
|
|
if(!($this->canBeEmpty && !$this->value)) {
|
|
|
|
parent::saveInto($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a pretty readonly field with some stars in it
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$stars = '*****';
|
|
|
|
|
|
|
|
$field = new ReadonlyField($this->name, $this->title ? $this->title : _t('Member.PASSWORD'), $stars);
|
|
|
|
$field->setForm($this->form);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
}
|