Merge pull request #2757 from jedateach/confirmedpasswordfield-fix

FIX: ConfirmedPasswordField setName failed to set names of child fields.
This commit is contained in:
Will Rossiter 2014-01-08 12:33:02 -08:00
commit 516277a6ad
2 changed files with 46 additions and 7 deletions

View File

@ -54,6 +54,13 @@ class ConfirmedPasswordField extends FormField {
*/ */
protected $showOnClick = false; protected $showOnClick = false;
/**
* A place to temporarly store the confirm password value
* @var string
*/
protected $confirmValue;
/** /**
* Title for the link that triggers the visibility of password fields. * Title for the link that triggers the visibility of password fields.
* *
@ -247,10 +254,8 @@ class ConfirmedPasswordField extends FormField {
$oldValue = $this->value; $oldValue = $this->value;
if(is_array($value)) { if(is_array($value)) {
//only set the value if it's valid!
if($this->validate(RequiredFields::create())) {
$this->value = $value['_Password']; $this->value = $value['_Password'];
} $this->confirmValue = $value['_ConfirmPassword'];
if($this->showOnClick && isset($value['_PasswordFieldVisible'])) { if($this->showOnClick && isset($value['_PasswordFieldVisible'])) {
$this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]') $this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]')
@ -274,6 +279,20 @@ class ConfirmedPasswordField extends FormField {
return $this; return $this;
} }
/**
* Update the names of the child fields when updating name of field.
*
* @param string $name new name to give to the field.
*/
public function setName($name) {
$this->children->fieldByName($this->getName() . '[_Password]')
->setName($name . '[_Password]');
$this->children->fieldByName($this->getName() . '[_ConfirmPassword]')
->setName($name . '[_ConfirmPassword]');
return parent::setName($name);
}
/** /**
* Determines if the field was actually shown on the client side - if not, * Determines if the field was actually shown on the client side - if not,
* we don't validate or save it. * we don't validate or save it.
@ -301,8 +320,8 @@ class ConfirmedPasswordField extends FormField {
$passwordField = $this->children->fieldByName($name.'[_Password]'); $passwordField = $this->children->fieldByName($name.'[_Password]');
$passwordConfirmField = $this->children->fieldByName($name.'[_ConfirmPassword]'); $passwordConfirmField = $this->children->fieldByName($name.'[_ConfirmPassword]');
$passwordField->setValue($_POST[$name]['_Password']); $passwordField->setValue($this->value);
$passwordConfirmField->setValue($_POST[$name]['_ConfirmPassword']); $passwordConfirmField->setValue($this->confirmValue);
$value = $passwordField->Value(); $value = $passwordField->Value();

View File

@ -4,6 +4,7 @@
* @subpackage tests * @subpackage tests
*/ */
class ConfirmedPasswordFieldTest extends SapphireTest { class ConfirmedPasswordFieldTest extends SapphireTest {
public function testSetValue() { public function testSetValue() {
$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA'); $field = new ConfirmedPasswordField('Test', 'Testing', 'valueA');
$this->assertEquals('valueA', $field->Value()); $this->assertEquals('valueA', $field->Value());
@ -52,4 +53,23 @@ class ConfirmedPasswordFieldTest extends SapphireTest {
$this->assertNotContains("showOnClick", $fieldHTML, $this->assertNotContains("showOnClick", $fieldHTML,
"Test class for hiding/showing the form contents is set"); "Test class for hiding/showing the form contents is set");
} }
public function testValidation() {
$field = new ConfirmedPasswordField('Test', 'Testing', array(
"_Password" => "abc123",
"_ConfirmPassword" => "abc123"
));
$validator = new RequiredFields();
$form = new Form($this, 'Form', new FieldList($field), new FieldList(), $validator);
$this->assertTrue($field->validate($validator));
$field->setName("TestNew"); //try changing name of field
$this->assertTrue($field->validate($validator));
//non-matching password should make the field invalid
$field->setValue(array(
"_Password" => "abc123",
"_ConfirmPassword" => "123abc"
));
$this->assertFalse($field->validate($validator));
}
} }