uuid-doctrine: Symfony form validation error

I’m using a uuid_binary field as primary key for a Doctrine Entity:

class Site
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     * @ORM\Column(type="uuid_binary")
     */
    private $id;

    ...
} 

I need to build a form for another entity which has that entity in a ManyToOne relation.

So I added, in my form builder, an EntityType as follows:

$builder->add('site')

Everything works, the HTML select has the uuids as keys, but when I submit the form I get this validation error:

Symfony\Component\Validator\ConstraintViolation Object(Symfony\Component\Form\Form).children[site] = 4a41b3c4-4299-11e6-b562-fc3fdb5f816d

Caused by: Symfony\Component\Form\Exception\TransformationFailedException Unable to reverse value for property path “site”: The choice “4a41b3c4-4299-11e6-b562-fc3fdb5f816d” does not exist or is not unique

Caused by: Symfony\Component\Form\Exception\TransformationFailedException The choice “4a41b3c4-4299-11e6-b562-fc3fdb5f816d” does not exist or is not unique

Any idea about how to solve this?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 17 (5 by maintainers)

Most upvoted comments

Sorry to revive this old thread, but I have better solution than the custom UuidEntityType proposed by @umpirsky (thanks for that btw) and people reading this thread may be interested.

Only thing you have to do is pass your entity repository into the form type and pass choices inside with $repository->findAll();.

Like this:

final class SomeFormType extends AbstractType
{
    private SportRepository $sportRepository;

    public function __construct(
        SportRepository $sportRepository
    ) {
        $this->sportRepository = $sportRepository;
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add(
                'sport',
                EntityType::class,
                [
                    'required' => true,
                    'class' => Sport::class,
                    'choice_label' => 'name',
                    'label' => 'Sport',
                    'choices' => $this->sportRepository->findAll(),
                ]
            );
    }
}

I found a workaround by creating custom UuidEntityType, here is the gist.

@ramsey I’d like to contribute this, but not sure where it belongs? ORMQueryBuilderLoader.php knows only about uuid and guid, but not about uuid_binary_ordered_time. Do you think it’s worth adding optionl Symfony compatibility layer to this package?

Is the form submitting a UUID and attempting to do a database lookup based on that ID? If so, check to make sure the form isn’t using the string version of the UUID when trying to do the database lookup. It needs to use the binary version of the UUID.