Change form before submitting it

0

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.

    
asked by anonymous 16.06.2016 / 18:32

1 answer

0

When I run into problems where I can not display values in my application forms, what I do is simply fill in those values after the submitted form has been validated.

So 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();
}
    
22.06.2016 / 08:47