Created
February 2, 2016 09:51
-
-
Save bummzack/12280fd26367490b99b8 to your computer and use it in GitHub Desktop.
SilverStripe Member signup page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Fix for issue https://github.com/silverstripe/silverstripe-framework/issues/2496 | |
*/ | |
class FixedConfirmedPasswordField extends ConfirmedPasswordField | |
{ | |
protected $useMemberValidator = false; | |
public function getUseMemberValidator() | |
{ | |
return $this->useMemberValidator; | |
} | |
public function setUseMemberValidator($value) | |
{ | |
$this->useMemberValidator = $value == true; | |
return $this; | |
} | |
public function setValue($value, $data = null) { | |
// If $data is a DataObject, don't use the value, since it's a hashed value | |
if ($data && $data instanceof DataObject) $value = ''; | |
//store this for later | |
$oldValue = $this->value; | |
if(is_array($value)) { | |
$this->value = $value['_Password']; | |
$this->confirmValue = $value['_ConfirmPassword']; | |
if($this->showOnClick && isset($value['_PasswordFieldVisible'])) { | |
$this->children->fieldByName($this->getName() . '[_PasswordFieldVisible]') | |
->setValue($value['_PasswordFieldVisible']); | |
} | |
} else { | |
if($value || (!$value && $this->canBeEmpty)) { | |
$this->value = $value; | |
} | |
} | |
//looking up field by name is expensive, so lets check it needs to change | |
if ($oldValue != $this->value) { | |
$this->children->fieldByName($this->getName() . '[_Password]') | |
->setValue($this->value); | |
$this->children->fieldByName($this->getName() . '[_ConfirmPassword]') | |
->setValue($this->confirmValue); | |
} | |
return $this; | |
} | |
public function validate($validator) | |
{ | |
// Use the member password validator for this field. | |
// This way the validation happens on form-level and not when attempting to save the member to the DB. | |
if($this->useMemberValidator){ | |
/** @var PasswordValidator $pwValidator */ | |
$pwValidator = Member::password_validator(); | |
if($pwValidator){ | |
/** @var ValidationResult $result */ | |
$result = $pwValidator->validate($this->value, null); | |
if(!$result->valid()){ | |
$validator->validationError( | |
$this->name, | |
$result->message(), | |
"validation", | |
false | |
); | |
return false; | |
} | |
} | |
} | |
return parent::validate($validator); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SignupPage extends Page | |
{ | |
private static $description = 'Member signup page.'; | |
} | |
class SignupPage_Controller extends Page_Controller | |
{ | |
private static $allowed_actions = array( | |
'AccountRegisterForm' | |
); | |
public function AccountRegisterForm() | |
{ | |
$fields = FieldList::create(array( | |
TextField::create('FirstName', _t('Member.FIRSTNAME', 'Firstname')), | |
TextField::create('Surname', _t('Member.SURNAME', 'Surname')), | |
EmailField::create('Email', _t('Member.EMAIL', 'Email')), | |
// Use a fixed password field because of https://github.com/silverstripe/silverstripe-framework/issues/2496 | |
$pwField = FixedConfirmedPasswordField::create('Password', _t('Member.PASSWORD', 'Password')) | |
)); | |
$pwField->setUseMemberValidator(true)->setCanBeEmpty(false); | |
$validator = Member_Validator::create(array('FirstName', 'Surname', 'Email', 'Password')); | |
$actions = FieldList::create(array( | |
FormAction::create('doCreateAccount', _t('SignupPage.CREATE_ACCOUNT', 'Create')) | |
)); | |
$form = Form::create($this, 'AccountRegisterForm', $fields, $actions, $validator); | |
return $form; | |
} | |
public function doCreateAccount($data, $form) | |
{ | |
$submittedData = $data; | |
if(is_array($submittedData['Password'])){ | |
// confirmed password field stores the field value in an array which will cause | |
// errors upon creating the new Member. | |
$submittedData['Password'] = $data['Password']['_Password']; | |
} | |
/** @var Member $member */ | |
$member = Member::create($submittedData); | |
try { | |
$member->write(); | |
if($group = SiteConfig::current_site_config()->CustomerGroup()){ | |
$member->Groups()->add($group); | |
} | |
$member->logIn(); | |
if($accountPage = $this->AccountPage()){ | |
return $this->redirect($accountPage->AbsoluteLink()); | |
} | |
$form->sessionMessage(_t('SignupPage.USER_CREATED', 'The account was successfully created'), 'good'); | |
} catch(Exception $e){ | |
$form->sessionMessage($e->getMessage(), 'bad'); | |
} | |
return $this->redirectBack(); | |
} | |
public function AccountPage() | |
{ | |
return AccountPage::get()->first(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment