form-> getData()不检索datepicker。 $ request-> request-> get does

时间:2016-02-23 11:07:54

标签: ajax forms symfony

我有一个带有(多个)datepicker(jquery)的表单。

当我提交表单时,$ form-> getData()不会返回日期的值。并且$ form-> isValid()返回false。我设置的任何类型的值,getData()和isValid()都是false!

但是,当我使用它时:

$postData = $request->request->get('place_edit_'.$placeId);

它返回所有数据,日期值很好!

$postData = $request->request->get('euro2016_place_edit_'.$placeId);
$em = $this->getDoctrine()->getManager();
// some code here

// get place to update
$repository = $em->getRepository('xxx:xxx:xxx');
$place = $repository->find($placeId);
$form = $this->createForm(new PlaceType($em, $place, $stadium), $place);
$form->handleRequest($request);
// some code here
$formPlace = $form->getData();
/*
 * $formPlace->setBirthDate(new \DateTime($postData['birthDate']));
 * $em = $this->getDoctrine()->getManager();
 * $em->persist($formPlace);
 * $em->flush();
 */
 if ($form->isValid()) {

表单构建器:

use ... ... Form\Type\BasicPlaceType;

/**
 * Class PlaceType
 */
class PlaceType extends BasicPlaceType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->add('save', 'submit', array(
                'label' => 'content.save',
                'attr' => array(
                'value' => 'save',
                ),
            ))
            ->add('submit', 'submit', array(
                'validation_groups' => array('Strict'),
                'label' => 'content.submit',
                'attr' => array(
                'value' => 'submit',
                ),
            ))
            ->add('actionType', 'hidden', array(
                'mapped' => false,
            ))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => '\Entity\Place',
            'attr' => ['id' => $this->getName()."_form"],
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'place_edit_'.$this->place->getId();
    }
}

和BasicPlaceType的部分:

...
                ->add('birthDate', 'genemu_jquerydate', array(
                'required' => false,
                'label'    => 'content.birthdate',
                'widget'   => 'single_text',
                'mapped'   => true,
                'format'   => 'dd-MM-yyyy',
                'attr' => $attr,
            ))
...

所以,我的问题是:为什么getData()不返回值(而且isValid()返回false)

1 个答案:

答案 0 :(得分:0)

了解formBuilder和DataLocked。 检查答案是否有类似的问题: https://stackoverflow.com/a/18878858/5269474