Skip to content

Upgrade information for the choice_value option #16848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions UPGRADE-2.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,41 @@ Form
}
```

* In Symfony 2.8 a small BC break was introduced with the new choices_as_values
option. In order to have the choice values populated to the html value attribute
you had to define the choice_value option. This is now not any more needed.

Before:

```php
$form->add('status', 'choice', array(
'choices' => array(
'Enabled' => Status::ENABLED,
'Disabled' => Status::DISABLED,
'Ignored' => Status::IGNORED,
),
'choices_as_values' => true,
// important if you rely on your option value attribute (e.g. for JavaScript)
// this will keep the same functionality as before
'choice_value' => function ($choice) {
return $choice;
},
));
```

After (Symfony 2.8+):

```php
$form->add('status', 'choice', array(
'choices' => array(
'Enabled' => Status::ENABLED,
'Disabled' => Status::DISABLED,
'Ignored' => Status::IGNORED,
),
'choices_as_values' => true
));
```

* Returning type instances from `FormTypeInterface::getParent()` is deprecated
and will not be supported anymore in Symfony 3.0. Return the fully-qualified
class name of the parent type class instead.
Expand Down
35 changes: 35 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,41 @@ UPGRADE FROM 2.x to 3.0
}
}
```

* In Symfony 2.8 a small BC break was introduced with the new choices_as_values
option. In order to have the choice values populated to the html value attribute
you had to define the choice_value option. This is now not any more needed.

Before:

```php
$form->add('status', 'choice', array(
'choices' => array(
'Enabled' => Status::ENABLED,
'Disabled' => Status::DISABLED,
'Ignored' => Status::IGNORED,
),
// choices_as_values will default to true in Symfony 3.0
'choices_as_values' => true,
// important if you rely on your option value attribute (e.g. for JavaScript)
// this will keep the same functionality as before
'choice_value' => function ($choice) {
return $choice;
},
));
```

After:

```php
$form->add('status', 'choice', array(
'choices' => array(
'Enabled' => Status::ENABLED,
'Disabled' => Status::DISABLED,
'Ignored' => Status::IGNORED,
)
));
```

* The `request` service was removed. You must inject the `request_stack`
service instead.
Expand Down
74 changes: 51 additions & 23 deletions src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@
*/
class EntityUserProvider implements UserProviderInterface
{
private $registry;
private $managerName;
private $classOrAlias;
private $class;
private $repository;
private $property;
private $metadata;

public function __construct(ManagerRegistry $registry, $class, $property = null, $managerName = null)
public function __construct(ManagerRegistry $registry, $classOrAlias, $property = null, $managerName = null)
{
$em = $registry->getManager($managerName);
$this->class = $class;
$this->metadata = $em->getClassMetadata($class);

if (false !== strpos($this->class, ':')) {
$this->class = $this->metadata->getName();
}

$this->repository = $em->getRepository($class);
$this->registry = $registry;
$this->managerName = $managerName;
$this->classOrAlias = $classOrAlias;
$this->property = $property;
}

Expand All @@ -51,18 +46,19 @@ public function __construct(ManagerRegistry $registry, $class, $property = null,
*/
public function loadUserByUsername($username)
{
$repository = $this->getRepository();
if (null !== $this->property) {
$user = $this->repository->findOneBy(array($this->property => $username));
$user = $repository->findOneBy(array($this->property => $username));
} else {
if (!$this->repository instanceof UserLoaderInterface) {
if (!$this->repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($this->repository)));
if (!$repository instanceof UserLoaderInterface) {
if (!$repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($repository)));
}

@trigger_error('Implementing loadUserByUsername from Symfony\Component\Security\Core\User\UserProviderInterface is deprecated since version 2.8 and will be removed in 3.0. Implement the Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface instead.', E_USER_DEPRECATED);
}

$user = $this->repository->loadUserByUsername($username);
$user = $repository->loadUserByUsername($username);
}

if (null === $user) {
Expand All @@ -77,26 +73,28 @@ public function loadUserByUsername($username)
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof $this->class) {
$class = $this->getClass();
if (!$user instanceof $class) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}

if ($this->repository instanceof UserProviderInterface) {
$refreshedUser = $this->repository->refreshUser($user);
$repository = $this->getRepository();
if ($repository instanceof UserProviderInterface) {
$refreshedUser = $repository->refreshUser($user);
} else {
// The user must be reloaded via the primary key as all other data
// might have changed without proper persistence in the database.
// That's the case when the user has been changed by a form with
// validation errors.
if (!$id = $this->metadata->getIdentifierValues($user)) {
if (!$id = $this->getClassMetadata()->getIdentifierValues($user)) {
throw new \InvalidArgumentException('You cannot refresh a user '.
'from the EntityUserProvider that does not contain an identifier. '.
'The user object has to be serialized with its own identifier '.
'mapped by Doctrine.'
);
}

$refreshedUser = $this->repository->find($id);
$refreshedUser = $repository->find($id);
if (null === $refreshedUser) {
throw new UsernameNotFoundException(sprintf('User with id %s not found', json_encode($id)));
}
Expand All @@ -110,6 +108,36 @@ public function refreshUser(UserInterface $user)
*/
public function supportsClass($class)
{
return $class === $this->class || is_subclass_of($class, $this->class);
return $class === $this->getClass() || is_subclass_of($class, $this->getClass());
}

private function getObjectManager()
{
return $this->registry->getManager($this->managerName);
}

private function getRepository()
{
return $this->getObjectManager()->getRepository($this->classOrAlias);
}

private function getClass()
{
if (null === $this->class) {
$class = $this->classOrAlias;

if (false !== strpos($class, ':')) {
$class = $this->getClassMetadata()->getName();
}

$this->class = $class;
}

return $this->class;
}

private function getClassMetadata()
{
return $this->getObjectManager()->getClassMetadata($this->classOrAlias);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function testLoadUserByUserNameShouldDeclineInvalidInterface()
private function getManager($em, $name = null)
{
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$manager->expects($this->once())
$manager->expects($this->any())
->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($em));
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public function createNamedBuilder($name, $type = 'Symfony\Component\Form\Extens
}

if ($type instanceof FormTypeInterface) {
@trigger_error('Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
@trigger_error(sprintf('Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead (%s).', get_class($type)), E_USER_DEPRECATED);
$type = $this->resolveType($type);
} elseif (is_string($type)) {
$type = $this->registry->getType($type);
} elseif ($type instanceof ResolvedFormTypeInterface) {
@trigger_error('Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
@trigger_error(sprintf('Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead (%s).', get_class($type->getInnerType())), E_USER_DEPRECATED);
} else {
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
}
Expand Down