Alexis_D
Alexis_D

Reputation: 1948

How to modify value from a form request before it is submitted in the DB?

I use an eventlisterner to check some values returned by the user/author before actually submitting them to the DB.

My form allow ROLE_USERS to change some attribut (like statut) to the Entity Article. Article has attribute such as statut(Published, Delete) and tobecheked('author wants to delete this article', 'null') which allow ROLE_ADMIN to review easily some article.

Anyway. I want that when Author submit statut=>'DELETE' the value of statut tobechecked to be turned into => 'author wants to delete this article'.

Here what I tried but this is not working.

class Article extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder->add('Statut', ...);
            ->add('toBeChecked',  ...);

    $dynamicSubmissionFunc = function (FormEvent $event)
    {
       $data = $event->getData();
       if($data['statut'] == 'DELETE')
       {
           // HOW CAN I MODIFY HERE THE VALUE OF ARTICLE ATTRIBUTE
           $data['toBeChecked'] = 'author wants to delete this article';
           // nothing is changed in the attribute toBeChecked.
       }
    }
    $builder->addEventListener(FormEvents::PRE_SUBMIT, $dynamicSubmissionFunc);
  }
}

Upvotes: 1

Views: 4046

Answers (1)

Michael Sivolobov
Michael Sivolobov

Reputation: 13240

Ok. I figured out. You need to set your data back to the event:

$event->setData($data);

Upvotes: 3

Related Questions