Form/Type/ResourceType.php
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // The view must always be an ID
        $builder->addViewTransformer(
            new CallbackTransformer(
                function ($originalData) {
                    if (!$originalData instanceof ResourceInterface) {
                        return $originalData; // Why is this condition necessary when submitting ? Makes no sense...
                    }

                    return $this->getIdentifierValue($originalData);
                },
                function ($submittedData) {
                    return $submittedData;
                }
            )
        );

        // Model data must always be an entity
        $builder->addModelTransformer(
            new CallbackTransformer(
                function ($originalData) {
                    return $originalData;
                },
                function ($submittedData) use ($options) {
                    /** @var ObjectRepository $repository */
                    $repository = $options['repository'];
                    if (null === $submittedData || '' === $submittedData) {
                        return null;
                    }

                    return $repository->find($submittedData);
                }
            )
        );
    }
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // The view must always be an ID
        $builder->addViewTransformer(
            new CallbackTransformer(
                function ($originalData) {
                    if (!$originalData instanceof ResourceInterface) {
                        return $originalData; // Why is this condition necessary when submitting ? Makes no sense...
                    }

                    return $this->getIdentifierValue($originalData);
                },
                function ($submittedData) {
                    return $submittedData;
                }
            )
        );

        // Model data must always be an entity
        $builder->addModelTransformer(
            new CallbackTransformer(
                function ($originalData) {
                    return $originalData;
                },
                function ($submittedData) use ($options) {
                    /** @var ObjectRepository $repository */
                    $repository = $options['repository'];
                    if (null === $submittedData || '' === $submittedData) {
                        return null;
                    }

                    return $repository->find($submittedData);
                }
            )
        );
    }
public function buildForm ( FormBuilderInterface $ builder , array $ options ) { // The view must always be an ID $ builder -> addViewTransformer ( new CallbackTransformer ( function ( $ originalData ) { if ( ! $ originalData instanceof ResourceInterface ) { return $ originalData ; // Why is this condition necessary when submitting ? Makes no sense... } return $ this -> getIdentifierValue ( $ originalData ) ; } , function ( $ submittedData ) { return $ submittedData ; } ) ) ; // Model data must always be an entity $ builder -> addModelTransformer ( new CallbackTransformer ( function ( $ originalData ) { return $ originalData ; } , function ( $ submittedData ) use ( $ options ) { /** @var ObjectRepository $repository */ $ repository = $ options [ 'repository' ] ; if ( null === $ submittedData || '' === $ submittedData ) { return null ; } return $ repository -> find ( $ submittedData ) ; } ) ) ; }
@param FormBuilderInterface $builder
@param array                $options

@throws \Exception
@param FormBuilderInterface $builder @param array $options