我的function
中有controller
可以让我编辑餐馆页面。
我需要setData
一些值,以便将它们从显示视图保留到编辑视图,我需要在city
和zipCode
字段中使用它。
$editForm->get('cityName')->setData($restaurant->getCity()->getName());
$editForm->get('cityZipCode')->setData($restaurant->getCity()->getZipCode());
但是,当我使用setData
时,我无法再编辑我的表单,因为出现此错误消息
You cannot change the data of a submitted form.
这是控制器部分
public function editAction(Request $request, Restaurant $restaurant)
{
$deleteForm = $this->createDeleteForm($restaurant);
$editForm = $this->createForm(RestaurantType::class, $restaurant);
$editForm->handleRequest($request);
$editForm->get('cityName')->setData($restaurant->getCity()->getName());
$editForm->get('cityZipCode')->setData($restaurant->getCity()->getZipCode());
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$cityManager = $this->container->get('admin.city_manager');
$cityManager->newCity($editForm, $restaurant);
$em->flush();
return $this->redirectToRoute('admin_restaurant_show_fr', array('id' => $restaurant->getId()));
}
return $this->render('admin/restaurant/edit.html.twig', array(
'restaurant' => $restaurant,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
这是FormType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'label' => 'Nom',
))
->add('latitude', NumberType::class, array(
'label' => 'Latitude',
))
->add('longitude', NumberType::class, array(
'label' => 'Longitude',
))
->add('address', TextType::class, array(
'label' => 'Adresse',
))
->add('cityName', TextType::class, array(
'mapped' => false,
'label' => 'Ville'
))
->add('cityZipCode', TextType::class, array(
'mapped' => false,
'label' => 'Code Postal'
))
}
如果您有任何可以帮助我解决问题的事情,我们将不胜感激。
答案 0 :(得分:2)
创建表单并调用handleRequest
后,Symfony会使用该版本的表单进行处理(例如,验证等)。
如果您在此之后更改表单,Symfony将不会高兴。
根据您要执行的操作以及您如何定义实体/表单,使用validation groups或dynamic form events来处理任何更改。
根据您所显示的内容,为什么不修改表格以显示餐厅的城市名称,而不是试图将其设置在控制器中。
例如;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'label' => 'Nom',
))
->add('latitude', NumberType::class, array(
'label' => 'Latitude',
))
->add('longitude', NumberType::class, array(
'label' => 'Longitude',
))
->add('address', TextType::class, array(
'label' => 'Adresse',
));
// if you want the fields not shown on the form
if (! empty($options['data']->getCity()) {
$builder->add('cityName', TextType::class, array(
'data' => $options['data']->getCity()->getName()
'mapped' => false,
'label' => 'Ville'
))
->add('cityZipCode', TextType::class, array(
'data' => $options['data']->getCity()->getZipCode()
'mapped' => false,
'label' => 'Code Postal'
));
}
// or if you want them there
$cityName = '';
$cityZipCode = '';
if (! empty($options['data']->getCity()) {
$cityName = $options['data']->getCity()->getName();
$cityZipCode = $options['data']->getCity()->getZipCode();
}
$builder->add('cityName', TextType::class, array(
'data' => $cityName
'mapped' => false,
'label' => 'Ville'
))
->add('cityZipCode', TextType::class, array(
'data' => $cityZipCode
'mapped' => false,
'label' => 'Code Postal'
));
}
}