Form doesn't get submitted

时间:2015-07-28 16:38:07

标签: php symfony

I have started to study Symfony2 since I will probably need in my work.

routing.yml:

account_register:
    path: /register
    defaults: {_controller: AppBundle:Register:index}

RegisterController:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\User;

class RegisterController extends Controller
{
    /**
     * @Route("/register")
     */
    public function indexAction(Request $request)
    {
        $register = new User();

        $form = $this->createFormBuilder($register)
            ->add('email', 'email', array('required' => false))
            ->add('password', 'password', array('required' => false))
            ->add('alias', 'text', array('required' => false))
            ->add('register', 'submit', array('label' => 'Register'))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isValid()) {
            // perform some action, such as saving the task to the database

            $em = $this->getDoctrine()->getManager();
            $em->persist($register);
            $em->flush();

            exit("Error");
            //return $this->redirectToRoute('task_success');
        }

        return $this->render('pages/register.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

regiser.html.twig:

{% extends 'base.html.twig' %}

{% block body %}
    <br /><br />
<div class = "window">
<form>

            <br /><br /><br /><br /><br /><br /><br />
            {{ form_start(form) }}

            {{ form_errors(form) }}

            <div id=center-text>Email</div>
            <div class="textfield" id=center>    {{ form_widget(form.email, {'attr': {'class': 'textfield', 'size': '22', 'maxlength': '100'}}) }}  </div>

            <br />

            <div id=center-text>Password</div>
            <div class="textfield" id=center>    {{ form_widget(form.password, {'attr': {'class': 'textfield', 'size': '22', 'maxlength': '100'}}) }}  </div>

            <br />

            <div id=center-text>Alias</div>
            <div class="textfield" id=center>   {{ form_widget(form.alias, {'attr': {'class': 'textfield', 'size': '22', 'maxlength': '100'}}) }}</div>

            <br /><br />

            <br />
            <center>
                {{ form_widget(form.register, {'attr': {'class': 'button'}}) }}
            </center>
            {{ form_end(form) }}
</form>
{% endblock %}

When i press the submit button only the url changes from http://localhost/website/web/app_dev.php/register to http://localhost/website/web/app_dev.php/register?form%5Bemail%5D=&form%5Bpassword%5D=&form%5Balias%5D=&form%5Bregister%5D=&form%5B_token%5D=hd70y_KjUEY8v51dQjnjU0ZMTJ0BYOihurV6IcIvghY

2 个答案:

答案 0 :(得分:0)

What is happening is the expected form's flow. The form that you are submitting is redirecting to the same page.

  • If you don't want your form to be in GET, you can change it for POST :

    $form = $this->createFormBuilder($register)
         ->setMethod('POST')
         ->...
    

When you are redirected after submitting the form, $form->handleRequest($request) binds the request (your data submitted) with the form that is just created. $form->isValid() then checks if your data is valid according to your form.

  • If you don't see any change after being redirected, you should check if $form->isValid() returns true.

  • If you want to redirect your form to another controller method, you should create another resource and then set the action attribute of your form thanks to the setAction() method.

To sum up, everything that I just told you is written and explained on this page, and you definitely should read it ! :-).

答案 1 :(得分:0)

愚蠢的我,html是我的测试代码然后我添加了symfony {{blocks}}并忘了删除旧标签

相关问题