1

Using Symfony, version 2.3 and more recent, I want the user to click on a link to go to the edition page of an already existing entity and that the form which is displayed to be already validated, with each error associated to its corresponding field, i.e. I want the form to be validated before the form is submitted.

I followed this entry of the cookbook :

$form = $this->container->get('form.factory')->create(new MyEntityFormType, $myEntity, array('validation_groups' => 'my_validation_group'));
$form->submit($request->request->get($form->getName()));
if ($form->isValid()) {
    ...
}

But the form is not populated with the entity datas : all fields are empty. I tried to replace $request->request->get($form->getName()) with $myEntity, but it triggered an exception :

$myEntity cannot be used as an array in Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php

Does anyone know a method to feed the submit method with properly formatted datas so I can achieve my goal ? Note : I don't want Javascript to be involved.

dbourcet
  • 364
  • 5
  • 12
  • 2
    why should the form be validated BEFORE submission? – DonCallisto Jun 22 '15 at 15:30
  • That the way I want it to be. The user submit the create form, then if he go to edition, I want him to see all the errors before submiting the edition form. – dbourcet Jun 22 '15 at 15:59
  • error shown before submission (that prevents submission so) are handled with html5 or javascript. if you want to avoid javascript, you could use html5 defining constraints. looking to your code, could you show us what's `my_validation_group` is? – DonCallisto Jun 22 '15 at 16:30
  • 1
    @DonCallisto: the OP makes a valid point: often, it is practical (and cleaner) to have the validation rules in one single place (i.e. inside the form/entity). – Genti Saliu Jun 22 '15 at 21:26
  • @GentiSaliu well, I didn't diacussed about spread validations. I've only said that validate BEFORE posting (and he pratically said not in the browser) is no-sense – DonCallisto Jun 22 '15 at 21:28
  • 1
    Look, I don't really want to discuss about user experience. Plus, I agree with you on that, but this is not my decision. I have to do it this way. And I just want to know how. My point is not to use HTML or Javascript, but to use PHP to "simulate" the fact that the form has been submitted, so errors are associated with their respective fields and can be accessed, as usual, in the Twig template like this, `{{ form_errors(form.fieldName) }}`. – dbourcet Jun 23 '15 at 07:47

2 Answers2

3

In place of:

$form->submit($request->request->get($form->getName()));

Try:

$form->submit(array(), false);
redbirdo
  • 4,937
  • 1
  • 30
  • 34
1

You need to bind the the request to the form in order to fill the form with the submitted values, by using: $form->bind($request);

Here is a detailed explanation of what your code should look like:

//Create the form (you can directly use the method createForm() in your controller, it's a shortcut to $this->get('form.factory')->create() )
$form = $this->createForm(new MyEntityFormType, $myEntity, array('validation_groups' => 'my_validation_group'));

// Perform validation if post has been submitted (i.e. detection of HTTP POST method)
if($request->isMethod('POST')){

    // Bind the request to the form
    $form->bind($request);

    // Check if form is valid
    if($form->isValid()){

        // ... do your magic ...

    }

}

// Generate your page with the form inside
return $this->render('YourBundle:yourview.html.twig', array('form' => $form->createView() ) );
Youri_G
  • 219
  • 2
  • 9
  • The problem is still here : fields are not populated with the entity datas : all fields are empty. I don't really know why though. – dbourcet Jun 23 '15 at 08:29