Skip to content
This repository was archived by the owner on Jun 16, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

'form_elements' => array(
'factories' => array(
'BaconUser\Form\RegistrationForm' => 'BaconUser\Form\Factory\RegistrationFormFactory'
'BaconUser\Form\UserFieldset' => 'BaconUser\Form\Factory\UserFieldsetFactory',
'BaconUser\Form\User\RegistrationForm' => 'BaconUser\Form\Factory\RegistrationFormFactory'
)
),

Expand Down
6 changes: 2 additions & 4 deletions src/BaconUser/Form/Factory/RegistrationFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace BaconUser\Form\Factory;

use BaconUser\Form\RegistrationForm;
use BaconUser\Form\User\RegistrationForm;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -28,10 +28,8 @@ class RegistrationFormFactory implements FactoryInterface
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();
$options = $parentLocator->get('BaconUser\Options\UserOptions');

$form = new RegistrationForm($options);
$form->setHydrator($parentLocator->get('HydratorManager')->get('BaconUser\Hydrator\RegistrationHydrator'));
$form = new RegistrationForm();
$form->setInputFilter($parentLocator->get('InputFilterManager')->get('BaconUser\InputFilter\RegistrationFilter'));

return $form;
Expand Down
38 changes: 38 additions & 0 deletions src/BaconUser/Form/Factory/UserFieldsetFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* BaconUser
*
* @link http://github.com/Bacon/BaconUser For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace BaconUser\Form\Factory;

use BaconUser\Form\UserFieldset;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Service factory that instantiates {@see UserFieldset}.
*/
class UserFieldsetFactory implements FactoryInterface
{
/**
* createService(): defined by FactoryInterface.
*
* @see FactoryInterface::createService()
* @param ServiceLocatorInterface $serviceLocator
* @return UserFieldset
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();
$options = $parentLocator->get('BaconUser\Options\UserOptions');

$fieldset = new UserFieldset($options);
$fieldset->setHydrator($parentLocator->get('HydratorManager')->get('BaconUser\Hydrator\RegistrationHydrator'));

return $fieldset;
}
}
59 changes: 59 additions & 0 deletions src/BaconUser/Form/User/RegistrationForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* BaconUser
*
* @link http://github.com/Bacon/BaconUser For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace BaconUser\Form\User;

use BaconUser\Options\UserOptionsInterface;
use Zend\Form\Form;

/**
* Generic registration form.
*/
class RegistrationForm extends Form
{
public function __construct()
{
parent::__construct('registration-form');

$this->add(array(
'type' => 'BaconUser\Form\UserFieldset',
'name' => 'user',
'options' => array(
'use_as_base_fieldset' => true
)
));

$this->add(array(
'type' => 'Csrf',
'name' => 'csrf'
));

$this->add(array(
'type' => 'Submit',
'name' => 'submit',
'options' => array(
'label' => 'Register',
)
));

// Add specific registration elements

$userFieldset = $this->get('user');
$userFieldset->add(array(
'type' => 'Password',
'name' => 'password_verification',
'options' => array(
'label' => 'Verify password'
),
'attributes' => array(
'required' => 'required'
)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
namespace BaconUser\Form;

use BaconUser\Options\UserOptionsInterface;
use Zend\Form\Form;
use Zend\Form\Fieldset;

/**
* Generic registration form.
* Base fieldset for user
*/
class RegistrationForm extends Form
class UserFieldset extends Fieldset
{
/**
* Constructor
*
* @param UserOptionsInterface $options
*/
public function __construct(UserOptionsInterface $options)
{
parent::__construct(null);
parent::__construct('user');

if ($options->getEnableUsername()) {
$this->add(array(
Expand All @@ -28,19 +33,20 @@ public function __construct(UserOptionsInterface $options)
'label' => 'Username',
),
'attributes' => array(
'type' => 'text',
'required' => 'required',
),
));
}

$this->add(array(
'type' => 'Email',
'name' => 'email',
'options' => array(
'label' => 'Email',
),
'attributes' => array(
'type' => 'text'
),
'required' => 'required'
)
));

if ($options->getEnableDisplayName()) {
Expand All @@ -50,41 +56,20 @@ public function __construct(UserOptionsInterface $options)
'label' => 'Display name',
),
'attributes' => array(
'type' => 'text'
'required' => 'required'
),
));
}

$this->add(array(
'type' => 'Password',
'name' => 'password',
'options' => array(
'label' => 'Password',
),
'attributes' => array(
'type' => 'password'
),
));

$this->add(array(
'name' => 'password_verification',
'options' => array(
'label' => 'Verify password',
),
'attributes' => array(
'type' => 'password'
),
));

$this->add(array(
'name' => 'submit',
'options' => array(
'label' => 'Register',
),
'attributes' => array(
'type' => 'submit',
),
), array(
'priority' => -100
'required' => 'required'
)
));
}
}
81 changes: 14 additions & 67 deletions src/BaconUser/InputFilter/RegistrationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
namespace BaconUser\InputFilter;

use BaconUser\Options\UserOptionsInterface;
use Zend\InputFilter\InputFilter;
use Zend\Validator\ValidatorInterface;

/**
* Input filter for the {@see RegistrationForm}.
*/
class RegistrationFilter extends InputFilter
class RegistrationFilter extends UserFilter
{
/**
* @param ValidatorInterface $emailUniqueValidator
Expand All @@ -28,85 +27,33 @@ public function __construct(
ValidatorInterface $usernameUniqueValidator,
UserOptionsInterface $options
) {
if ($options->getEnableUsername()) {
$this->add(array(
'name' => 'username',
'required' => true,
'filters' => array(array('name' => 'StringTrim')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => 3,
'max' => 255,
),
),
$usernameUniqueValidator,
),
));
}

$this->add(array(
'name' => 'email',
'required' => true,
'filters' => array(array('name' => 'StringTrim')),
'validators' => array(
array(
'name' => 'EmailAddress'
),
$emailUniqueValidator,
),
));

if ($options->getEnableDisplayName()) {
$this->add(array(
'name' => 'display_name',
'required' => false,
'filters' => array(array('name' => 'StringTrim')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => 3,
'max' => 255,
),
),
),
));
}
parent::__construct($options);

$this->add(array(
'name' => 'password',
'required' => true,
'filters' => array(array('name' => 'StringTrim')),
'name' => 'password_verification',
'required' => true,
'filters' => array(array('name' => 'StringTrim')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => 6,
),
),
),
));

$this->add(array(
'name' => 'password_verification',
'required' => true,
'filters' => array(array('name' => 'StringTrim')),
'validators' => array(
array(
'name' => 'StringLength',
'name' => 'StringLength',
'options' => array(
'min' => 6,
),
),
array(
'name' => 'Identical',
'name' => 'Identical',
'options' => array(
'token' => 'password',
),
),
),
));

// Add specific validation rules
$usernameValidators = $this->get('username')->getValidatorChain();
$usernameValidators->addValidator($usernameUniqueValidator);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bakura10 Why dont you just add inputs inside init() method? Then you'll have the updated ValidatorManager with UsernameUniqueValidator and EmailUniqueValidator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init() is not needed if you explicitly set dependencies like it is the case here. I don't know yet what to do.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But all filters and validators are dependencies. It makes no sense you explicitly set dependencies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. I'll change that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm in fact it's not so easy danizord, because the validator requires options… Not sure about how to do it. Plugin managers are always limited when it comes to option.. So I think the current solution is the best one for now :/.


$emailValidators = $this->get('email')->getValidatorChain();
$emailValidators->addValidator($emailUniqueValidator);
}
}
Loading