Change form before submitting it

Asked

Viewed 29 times

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.

1 answer

0

When I run into problems where I cannot expose values in my application forms, what I do is simply fill in these values after the submitted form has been validated.

Then the code would look like this.

Form:

class ClientType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->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,
        ));
    }
}

Controller:

$form = $this->createForm(ClientType::class, new Clients());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    $clients = $form->getData();
    $clients->setAppId($appId);
    $this-getDoctrine()->getManager()->persist($clients);
    $this-getDoctrine()->getManager()->flush();
}
  • 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.

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.