Validation of a form before submission

前端 未结 2 1871

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

相关标签:
2条回答
  • 2021-01-25 20:35

    In place of:

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

    Try:

    $form->submit(array(), false);
    
    0 讨论(0)
  • 2021-01-25 20:40

    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() ) );
    
    0 讨论(0)
提交回复
热议问题