有一種表單可以創建Chain實體。Symfony2:表單未創建
class ChainType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('label' => 'Company name'))
->add('logoImageURL', TextType::class, array('label' => 'Company logo'));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CoreBundle\Entity\Chain'
));
}
}
這裏是一個newAction創建表單和保存實體
/**
* Creates a new Chain entity.
*
* @Route("/new", name="chain_new")
*/
public function newAction(Request $request)
{
$chain = new Chain();
$form = $this->createForm(ChainType::class, $chain);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($chain);
$em->flush();
return $this->redirectToRoute('chain_show');
}
return $this->render(
'AdminBundle:ChainPanel:new.html.twig',
array('form' => $form->createView())
);
}
下面是一個按鈕來創建表單
<button type="button" class="btn btn-primary">
<a href="{{ path('chain_new') }}">
Add Chain
</a>
</button>
問題
當我點擊「添加鏈'按鈕表單沒有創建,我只是重定向t o'chain_show'路線。我的代碼有什麼問題?
更新 - 我的解決方案
在控制器我把newAction的showAction之前。這個固定的問題。然而我找不到解釋