Symfony2我无法从Twig模板提交有效表单

Symfony2我无法从Twig模板提交有效表单

问题描述:

I am trying to make a search by date form to work on Symfony 2.3. I have an entity with a few fields (5) the name of the entity is Schedule and two of this fields are datetime, for start date time and end Date time. I want to search by dates, but it is giving me headaches. I have this action:

    public function indexAction(Request $request)
{
    //time form creation
    $aSchedule = new Schedule();
    $dateTimeForm = $this->createFormBuilder($aSchedule)
            ->add('startDateTime', 'datetime')
            ->add('endDateTime', 'datetime')
            ->add('search', 'submit')
            ->getForm();
    //getting the formr using post
    $dateTimeForm->handleRequest($request);

    if ($dateTimeForm->isSubmitted()){
        echo 'Submited';
    }
    if ($dateTimeForm->isValid()){
        echo 'Is Valid';
    }
}

I have show the form in template like this:

        <form action="{{ path('osd_sch_homepage') }}" method="post" 
        {{ form_enctype(dateTimeForm) }} >
        <div id="start-date-time">
            {{ form_label(dateTimeForm.startDateTime) }}
            {{ form_errors(dateTimeForm.startDateTime) }}
            {{ form_widget(dateTimeForm.startDateTime) }}                    
        </div>         
        <div id="end-date-time">
            {{ form_label(dateTimeForm.endDateTime) }}
            {{ form_errors(dateTimeForm.endDateTime) }}
            {{ form_widget(dateTimeForm.endDateTime) }}            
        </div>
        <div>
            {{ form_widget(dateTimeForm.search) }}
        </div>
    </form>

Now in the action every time in send the form the "$dateTimeForm->isSubmitted()" works fine, but the "$dateTimeForm->isValid()" is not getting true, I mean is never going the "echo 'Is Valid';". what am I doing wrong? Thank you in advanced. Abel Guzman

It's probably not putting the auto generated CSRF token. Try putting {{ form_rest }} at the end.

Have you tried to debug your form errors?

foreach($form->getErrors() as $err){
     echo $err->getMessage();
}