Symfony表单失败isValid

时间:2014-03-10 20:10:56

标签: php forms symfony

我有一个奇怪的问题,我在Symfony中使用表单时无法调试。我有一个表格在很长一段时间内工作得很好,然后突然昨天它开始失败$form->isValid()检查但是没有使用$form->getErrors()$form->getErrorsAsString()返回任何错误。然而,如果我在调用\Doctrine\Common\Utils\Debug::dump($request->request->get('reportsettingstype')之前使用$form->isValid()它通过了检查,则会有一个奇怪的转折。那么现在真正的问题是,如何回应请求数据会导致表单突然通过验证?

我包含控制器操作和表单类以供参考:

/**
 * @Route("/admin/report/build", name="build_report")
 * @param \Symfony\Component\HttpFoundation\Request $request
 */
public function generateReportAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $report = new \Save\ReportBundle\Entity\Report;

    $form = $this->createForm( new \Save\ReportBundle\Form\Type\ReportSettingsType, $report );               

    $form->handleRequest($request);                                              

    //echo '<pre>'; \Doctrine\Common\Util\Debug::dump($request->request->get('reportsettingstype'), 2); echo '</pre>';
    if( $form->isValid() ){
        $data = $request->request->get('reportsettingstype');
        if( $data['newReport'] == 1){
            $date = new \DateTime(date('m/d/Y', time()));            
            $client = $em->getRepository('SaveClientBundle:Client')->find($data['client']);
            $title = strtolower(str_replace(' ', '_', $client->getName()).'_'.$date->format('m-d-Y'));
            $report->setDate($date);
            $report->setTitle($title);
            $report->setClient($client);
            $order = $em->getRepository('SaveOrderBundle:Orders')->find($data['order']);
            $report->setOrder($order);
            if( array_key_exists('reportFilter', $data)){
                $reportFilter = $em->getRepository('SaveClientBundle:ClientArea')->find($data['reportFilter']);
                $report->setReportFilter($reportFilter);
            }
            $assessments = $this->_findAssessments( $report );
            if( $assessments->count() === 0 ){
                $this->get('session')->getFlashBag()->add('error', 'No assessments found for area '.$report->getReportFilter()->getName());
                return $this->render('SaveReportBundle:Default:reporterror.html.twig', array('report' => $report));
            }
            $report->setAssessments( $assessments );
            $report->setTotals( $this->_calculateTotals( $report->getAssessments() ) );
            $report->setGrandTotals( $this->_calculateGrandTotals( $report->getTotals() ) );
            $report->setPossibles( $this->_calculatePossible( $report ) );
            $report->setCount( $report->getAssessments()->count() );

            if( $report->getId() != "" && null !== $report->getId() ){
                $em->merge($report);
            } else {
                $em->persist($report);
            }
            $em->flush();

            $pageMaker = $this->get('page_helper');
            $pageMaker->setReport($report);
            $pageMaker->cleanupActions();
            $pageMaker->createPages();
            $report = $pageMaker->getReport();
        } else {
            $report = $em->getRepository('SaveReportBundle:Report')->find($data['reportList']);
        }

        $pages = $report->getPages();
        $html = '';
        //$html .= $report->getReportHeader();
        if($pages->count() > 0){
            foreach($pages as $page){
                $html .= $page->getPageHeader() . $page->getPageBody() . $page->getPageFooter();                    
            }
        }         
        //$html .= $report->getReportFooter();

        return $this->render('SaveReportBundle:Default:reportview.html.twig', array('reportData' => $html, 'report'=>$report));
    } else {
        //$errors = $this->getErrorMessages($form);                                                            
        $errors = $form->getErrorsAsString();
        $errors2 = $this->getErrorMessages($form);
        die('<pre>Errors: '.var_dump($errors).'</pre>'.var_dump($errors2));
    }

    return $this->render('SaveCoreBundle:Default:debugData.html.twig', array('data'=>array('message'=>'No Data Supplied')));
}

表格类

class ReportSettingsType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $defaultArray = array();
        for($x=0; $x<999; $x++){
            $defaultArray[$x]='';
        }

        $builder->add('client', 'entity', array(
                'class'=>'Save\ClientBundle\Entity\Client',
                'property'=>'name',
                'label'=>'Client: ',
                'empty_value' => '-- Select Client --'
            ))
            ->add('newReport', 'choice', array(
                'choices' => array(
                    1 => 'Yes',
                    0 => 'No'
                ),
                'label' => 'Run New Report: ',
                'mapped' => false,
                'expanded' => true,
                'multiple' => false
            ))
            ->add('reportType', 'choice', array(
                'choices' => array(
                    'aggregate' => 'Aggregate',
                    'filtered' => 'Filtered'
                ),
                'label' => 'Report Type: ',
                'expanded' => true,
                'multiple' => false,
                'data' => 'aggregate'
            ))
            ->add('reportSections', 'choice', array(
                'choices' => array(
                    'Welcome Letter' => 'Welcome Letter Page',
                    'Report Details' => 'Report Details Page',
                    'Section Definitions' => 'Section Definitions',
                    'Status Summary Graphs' => 'Status Summary Graphs',
                    'Existing Conditions Graphs' => 'Existing Conditions Graphs',
                    'Engagement Action Plans' => 'Engagement Action Plans',
                    'Priority Action Plans' => 'Priority Action Plans',
                    'Employee Engagement Benchmarks' => 'Employee Engagement Benchmarks',
                    'Conclusion' => 'Future Discussions'
                ),
                'label' => 'Report Sections: ',
                'multiple' => true,
                'expanded' => true,
                'required' => false
            ))
            ->add('order', 'choice', array(                     
                'label' => 'Order: ',
                'mapped' => false,
                'choices' => $defaultArray,
                'empty_value' => '--Select Order--'
            ))
            ->add('reportFilter', 'choice', array(                     
                'label' => 'Report Filter: ',    
                'choices' => $defaultArray,
                'required' => false,    
                'mapped'=> false,
                'empty_value' => '-- Select Filter --'
            ))
            ->add('reportList', 'choice', array(
                'label' => 'Saved Reports: ',
                'choices' => $defaultArray,
                'required' => false,
                'mapped' => false,
                'empty_value' => '-- Select Report --'
            ))
            ->add('processReport', 'submit', array(
                'label' => 'Process Report',                    
            ));                

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
       $resolver->setDefaults(array(
           'data_class' => 'Save\ReportBundle\Entity\Report'
       ));
    }

    public function getParent()
    {
        return 'form';
    }

    public function getName()
    {
        return 'reportsettingstype';
    } 
}

如果有任何其他信息有用,请告诉我,我会将其添加到此问题中,现在我无法解释为什么表单未通过$form->isValid()检查。

0 个答案:

没有答案
相关问题