Skip to content

Commit

Permalink
Fixed validation groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Proietti committed Aug 20, 2012
1 parent 513c32e commit 4a35f51
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
45 changes: 42 additions & 3 deletions Manager/UserDiscriminator.php
Expand Up @@ -33,6 +33,10 @@ class UserDiscriminator

protected $class = null;

protected $registrationFormOptions = array();

protected $profileFormOptions = array();

/**
*
* @param ContainerInterface $serviceContainer
Expand Down Expand Up @@ -140,7 +144,7 @@ public function getRegistrationForm()
if (is_null($this->registrationForm)) {
$formFactory = $this->serviceContainer->get('form.factory');
$type = $this->getRegistrationFormType($this->getClass());
$this->registrationForm = $formFactory->createNamed($type->getName(), $type);
$this->registrationForm = $formFactory->createNamed($type->getName(), $type, null, $this->registrationFormOptions[$this->getClass()]);
}

return $this->registrationForm;
Expand All @@ -155,7 +159,7 @@ public function getProfileForm()
if (is_null($this->profileForm)) {
$formFactory = $this->serviceContainer->get('form.factory');
$type = $this->getProfileFormType($this->getClass());
$this->profileForm = $formFactory->createNamed($type->getName(), $type);
$this->profileForm = $formFactory->createNamed($type->getName(), $type, null, $this->profileFormOptions[$this->getClass()]);
}

return $this->profileForm;
Expand Down Expand Up @@ -186,6 +190,38 @@ protected function getProfileFormType($class)

return $type;
}

/**
* This function is needed due a bad bundle architecture.
* I would have had to use a MultiUser configuration with default values
*
* @param array $parameter
*/
protected function setRegistrationFormOptions(array $parameter)
{
if (!array_key_exists('registration_options', $parameter) || !array_key_exists('validation_groups', $parameter['registration_options'])) {
$this->registrationFormOptions[$parameter['entity']] = array('validation_groups' => array('Registration', 'Default'));
return;
}

$this->registrationFormOptions[$parameter['entity']] = $parameter['registration_options'];
}

/**
* This function is needed due a bad bundle architecture.
* I would have had to use a MultiUser configuration with default values
*
* @param array $parameter
*/
protected function setProfileFormOptions(array $parameter)
{
if (!array_key_exists('profile_options', $parameter) || !array_key_exists('validation_groups', $parameter['profile_options'])) {
$this->profileFormOptions[$parameter['entity']] = array('validation_groups' => array('Profile', 'Default'));
return;
}

$this->profileFormOptions[$parameter['entity']] = $parameter['profile_options'];
}

/**
*
Expand All @@ -208,7 +244,7 @@ protected function buildConfig(array $parameters)
$parameter[$key] = 'Nmn\MultiUserBundle\Manager\UserFactory';
}

if (!empty($val)) {
if (is_string($val) && !empty($val)) {
if (!class_exists($val)) {
throw new \LogicException(sprintf('Impossible build discriminator configuration: "%s" not found', $val));
}
Expand All @@ -219,6 +255,9 @@ protected function buildConfig(array $parameters)
$registrationFormTypes[$parameter['entity']] = $parameter['registration'];
$profileFormTypes[$parameter['entity']] = $parameter['profile'];
$userFactoriesTypes[$parameter['entity']] = $parameter['factory'];

$this->setRegistrationFormOptions($parameter);
$this->setProfileFormOptions($parameter);
}

$this->entities = $entities;
Expand Down
24 changes: 24 additions & 0 deletions Resources/doc/index.md
Expand Up @@ -212,6 +212,30 @@ parameters:
factory:
```

If you need to pass custom options to the form (like a validation groups)

``` yaml
# Acme/UserBundle/Resources/config/config.yml

parameters:
nmn_user_discriminator_parameters:
classes:
user_one:
entity: Acme\UserBundle\Entity\UserOne
registration: Acme\UserBundle\Form\Type\RegistrationUserOneFormType
registration_options:
validation_groups: [Registration, Default]
profile: Acme\UserBundle\Form\Type\ProfileUserOneFormType
profile_options:
validation_groups: [Profile, Default]
factory:
user_two:
entity: Acme\UserBundle\Entity\UserTwo
registration: Acme\UserBundle\Form\Type\RegistrationUserTwoFormType
profile: Acme\UserBundle\Form\Type\ProfileUserTwoFormType
factory:
```

### 7. Create your controllers

Nmn\MultiUserBundle\Controller\RegistrationController can handle registration flow only for
Expand Down
7 changes: 3 additions & 4 deletions Tests/Unit/Manager/UserDiscriminatorTest.php
Expand Up @@ -46,8 +46,7 @@ public function setUp()
* @return void
*/
public function testConstructor()
{

{
$reflectionClass = new \ReflectionClass("Nmn\MultiUserBundle\Manager\UserDiscriminator");

$entities = $reflectionClass->getProperty('entities');
Expand Down Expand Up @@ -151,7 +150,7 @@ public function testGetRegistrationForm()
'session',
'form.factory'))
->will($this->onConsecutiveCalls($formFactory, $this->session));
$formFactory->expects($this->exactly(1))->method('createNamed')->with('form_name', $type)->will($this->onConsecutiveCalls(null));
$formFactory->expects($this->exactly(1))->method('createNamed')->with('form_name', $type, null, array('validation_groups' => array('Registration', 'Default')))->will($this->onConsecutiveCalls(null));

$this->discriminator->getRegistrationForm();
}
Expand All @@ -167,7 +166,7 @@ public function testGetProfileForm()
'session',
'form.factory'))
->will($this->onConsecutiveCalls($formFactory, $this->session));
$formFactory->expects($this->exactly(1))->method('createNamed')->with('form_name', $type)->will($this->onConsecutiveCalls(null));
$formFactory->expects($this->exactly(1))->method('createNamed')->with('form_name', $type, null, array('validation_groups' => array('Profile', 'Default')))->will($this->onConsecutiveCalls(null));

$this->discriminator->getProfileForm();
}
Expand Down

0 comments on commit 4a35f51

Please sign in to comment.