0
There are 2 entities: Client and Apps.
The association between client and app is made in the entity Client
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 I don’t want to expose any id
in the form, then each app in addition to the id
, has the field reference
and it is this value that is sent by the form, see below the form build:
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 field reference
is false, that is, it does not exist in my entity but rather the field appId
.
By saving my form, the field appId
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 making the Ubmit form, I need to fill in the field appId
with a id
valid according to the reference sent through the field reference
.
Thanks for your help. The appid is not generic, as this is an endpoint common to multiple forms, each one has its ID, so this information comes from the form, I mean, I’m telling the system that there was a new record coming from form X, so I’m using a reference and not the ID itself.
– Filipe Moraes
However I solved the problem keeping the field "Reference" with the property "Mapped" equal to "false" and in the controller I make a "find" with the value filled, I check if the value is true and then set the app ID according to your answer.
– Filipe Moraes