There are 2 entities: Client and Apps.
The association between client and app is made in the Client
entity as follows:
class Clients {
/**
* App id
*
* @ManyToOne(targetEntity="Apps", inversedBy="clients", fetch="EXTRA_LAZY")
* @JoinColumn(name="app_id", referencedColumnName="id")
* @Assert\NotBlank(message="Código da aplicação inválido.")
*/
private $appId;
//...
}
The problem is that I do not want to display any id
on the form, so each app in addition to id
has the reference
field and this value is sent by the form, see below the form build: / p>
class ClientType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('reference', TextType::class, [
'mapped' => false
])
->add('title', TextType::class)
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('phone', TextType::class)
->add('email', EmailType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Clients',
'csrf_protection' => false,
));
}
}
Notice that the reference
field is false, that is, it does not exist in my entity, but the appId
field.
When saving my form, the appId
field goes with the value null
and the following validation error occurs (the message was defined via annotation in the above code):
Invalid application code.
In the controller, I have the following code:
//...
$data = json_decode($request->getContent(), true);
$client = new Clients();
$form = $this->createForm(ClientType::class, $client);
$form->submit($data);
if ($form->isValid()) {
//...
}
//...
Before submitting the form, I need to fill in the appId
field with a id
valid according to the reference sent through the reference
field.