验证symfony2中的动态表单返回"此值无效"

时间:2014-06-07 09:26:56

标签: forms validation symfony dynamic-forms

我有3个实体:Country,State和City,具有以下关系,我有另一个名为Location的实体,它具有用于设置用户位置的所有主题, 我也有一个表格,用于获取用户的位置数据。应该动态生成,因为实体彼此相关,例如,如果状态发生变化,城市也必须改变。

enter image description here

我在听众中使用了以下代码来执行此操作:

/**
 * @param FormFactoryInterface $factory
 * @param EntityManager $om
 */
public function __construct($factory, $om)
{
    $this->factory = $factory;
    $this->om = $om;
}

public static function getSubscribedEvents()
{
    return array(
        FormEvents::PRE_BIND => 'preBind',
        FormEvents::PRE_SET_DATA => 'preSetData',
    );
}

/**
 * @param event FormEvent
 */
public function preSetData(FormEvent $event)
{
    $company = $event->getData();

    if (null === $company)
    {
        return;
    }
    // company has country => edit action
    if($company->getCountry()){            

        //retrieve location
        $country = $company->getCountry();
        $state = $company->getState();
        $city = $company->getCity();

        if(is_object($state) && is_object($city) && is_object($country))
        {
            $states = $country->getStates();
            $cities = $state->getCities();
        }
        else
        {
            $cities = array();
            $states = array();
        }

    }else{
        // new location action
        $states = array();
        $cities = array();
    }

        $form = $event->getForm();
        $this->customizeForm($form, $cities,$states);

}

public function preBind(FormEvent $event)
{
    $dataForm = $event->getData();

    $countryId = $dataForm['country'];
    $stateId = $dataForm['state'];
    $cityId = $dataForm['city'];

    $country = $this->om
        ->getRepository('GMBundle:Country')
        ->find($countryId);
    $state = $this->om
        ->getRepository('GMBundle:State')
        ->find($stateId);
    $city = $this->om
        ->getRepository('GMBundle:City')
        ->find($cityId);

    if(is_object($state) && is_object($city) && is_object($country))
    {
        $states = $country->getStates();
        $cities = $state->getCities();
    }
    else
    {
        $cities = array();
        $states = array();
    }
    $form = $event->getForm();

    $this->customizeForm($form, $cities, $states);
}

protected function customizeForm($form, $cities, $states)
{
    $form
        ->add($this->factory->createNamed('state', 'entity', null, array(
                            'choices' => $states,
                            'label' => 'state',
                            'required' => true,
                            'empty_value' => 'choose state',
                            'class' => 'GMBundle:State',
                )))
        ->add($this->factory->createNamed('city', 'entity', null, array(
                'choices' => $cities,
                'label' => 'city',
                'required' => true,
                'empty_value' => 'choose city',
                'class' => 'GMBundle:City',
    )))
        ;
}

当我想编辑我的表单时,我收到了错误This value is not valid. 似乎symfony没有改变数据,例如,如果用户选择X作为Stated并且现在想为他的州选择Y,symfony说所选城市无效并且必须从X中选择城市,这是他以前的选择。

任何想法我怎么解决它?或者我做错了什么!?

-Thanks

0 个答案:

没有答案