From 813d34b15ef43167022454a3a99ecd331833836e Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Wed, 16 Oct 2013 11:29:43 +1300 Subject: [PATCH] FIX: Use Injector API for managing Member_Validator instance. Updates the CMS profile page and SecurityAdmin to give developers a few ways to customise the required fields. Added extension hook updateValidator for getValidator for things like modules to inject required fields to go along with Injector for replacing the entire class for project specific use. --- admin/code/CMSProfileController.php | 14 ++- admin/code/SecurityAdmin.php | 11 ++- forms/RequiredFields.php | 108 +++++++++++++++------ security/Member.php | 42 ++++++-- tests/control/CMSProfileControllerTest.php | 10 ++ tests/security/MemberTest.php | 100 ++++++++++++++++++- 6 files changed, 244 insertions(+), 41 deletions(-) diff --git a/admin/code/CMSProfileController.php b/admin/code/CMSProfileController.php index 2d751dbd1..6b03f105e 100644 --- a/admin/code/CMSProfileController.php +++ b/admin/code/CMSProfileController.php @@ -31,9 +31,9 @@ class CMSProfileController extends LeftAndMain { if($form instanceof SS_HTTPResponse) { return $form; } + $form->Fields()->removeByName('LastVisited'); $form->Fields()->push(new HiddenField('ID', null, Member::currentUserID())); - $form->setValidator(new Member_Validator()); $form->Actions()->push( FormAction::create('save',_t('CMSMain.SAVE', 'Save')) ->addExtraClass('ss-ui-button ss-ui-action-constructive') @@ -44,7 +44,17 @@ class CMSProfileController extends LeftAndMain { $form->Actions()->removeByName('action_delete'); $form->setTemplate('Form'); $form->setAttribute('data-pjax-fragment', null); - if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); + + if($member = Member::currentUser()) { + $form->setValidator($member->getValidator()); + } else { + $form->setValidator(Injector::inst()->get('Member')->getValidator()); + } + + if($form->Fields()->hasTabset()) { + $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); + } + $form->addExtraClass('member-profile-form root-form cms-edit-form cms-panel-padded center'); return $form; diff --git a/admin/code/SecurityAdmin.php b/admin/code/SecurityAdmin.php index 9a89badd1..2e71de0bc 100755 --- a/admin/code/SecurityAdmin.php +++ b/admin/code/SecurityAdmin.php @@ -75,7 +75,16 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider { ->addComponent(new GridFieldButtonRow('after')) ->addComponent(new GridFieldExportButton('buttons-after-left')) )->addExtraClass("members_grid"); - $memberListConfig->getComponentByType('GridFieldDetailForm')->setValidator(new Member_Validator()); + + if($record && method_exists($record, 'getValidator')) { + $validator = $record->getValidator(); + } else { + $validator = Injector::inst()->get('Member')->getValidator(); + } + + $memberListConfig + ->getComponentByType('GridFieldDetailForm') + ->setValidator($validator); $groupList = GridField::create( 'Groups', diff --git a/forms/RequiredFields.php b/forms/RequiredFields.php index 1e3adbd08..960a2bcde 100644 --- a/forms/RequiredFields.php +++ b/forms/RequiredFields.php @@ -1,10 +1,12 @@ required = array(); + return $this; } @@ -50,7 +55,9 @@ class RequiredFields extends Validator { * Debug helper */ public function debug() { - if(!is_array($this->required)) return false; + if(!is_array($this->required)) { + return false; + } $result = "