I have an entities called Organization and Person .

I have create a custom field type named OrganizationType which I add under PersonType :

class OrganizationType extends AbstractType { public function buildForm($builder) { $builder->add('name'); $builder->add('description'); } } class PersonType extends AbstractType { public function buildForm($builder) { $builder->add('fullName'); $builder->add('organization, new OrganizationType()); } }

This works fine for this submitted data structure:

{ "fullName" : "Justin Case", "organization" : { "name" : "FooBar Organization", "description" : "We do bar stuff." } }

In my application logic, this will create two entities: The Person entity and the Organization entity, with their association persisted as well.

However, I wish to support scalar values in form of an identifier for the organization field, too:

{ "fullName" : "Justin Case", "organization" : 5 }

The intended result is the Person entity is created, an an association to an existing Organization with ID 5 is linked and persisted as well.

However, it seems like a custom type can only support either a scalar value or non-scalar value, not both.

What I have tried:

I've experimented with attaching data transformers to OrganizationType ( $builder->addModelTransformer(...) and $builder->addViewTransformer(...) both) in hopes that I can reformat the submitted data from scalar to non-scalar (or vice-versa) depending on what my custom type expects but sadly it doesn't work -- if the custom type expects scalar, passing in a non-scalar value will result to my transformers not being called at all. This is true for both view transformers and model transformers)

Is this use-case for the Form component supported at all? Or is there a solution for this?