我需要帮助我创建的表单,我在一个 twig 文件中创建了 5 个表单并创建了一个控制器,我如何将我的值从表单绑定到数据库,当我现在绑定时,它只显示一个值表单 5 次,但我需要 5 次才能获得不同的价值 :(,请帮忙,我已经整天坚持这件事了..
我的树枝文件:
<div class="new-test">
<h2>New test </h2>
<form action="{{ path('test.create') }}" method="post">
Test name: <input type="text" name="name"/><br>
Category 1<input type="text" name="category-new" >
<div id="customWidget">
<div id="colorSelector1"><div style="background-color: #00ff00"></div>
</div>
<div id="colorpickerHolder1"></div>
</div>
Category 2<input type="text" name="category-new" ><br>
Category 3<input type="text" name="category-new" ><br>
Category 4<input type="text" name="category-new" ><br>
Category 5<input type="text" name="category-new" ><br>
<input type="submit" value="Add">
</form>
我的控制器:
/**
* @Route("/add/test", requirements={"name" = "\s+"}, name="test.create")
* @Method("Post")
* @return array
*/
public function createAction()
{
$success = 0;
$name = $this->getRequest()->get('name');
if( !empty($name) )
{
$test = new Test();
$test->setName($this->getRequest()->get('name'));
$em = $this->getDoctrine()->getManager();
$em->persist($test);
$em->flush();
$success = 'Test '.$test->getName().' was created';
}
else
{
$success = 'Test name can not be empty';
}
$category = $this->getRequest()->get('category-new');
for ($i=0; $i<=5; $i++){
if( !empty($category) )
{
$categoryName = new Category();
$categoryName->setName($this->getRequest()->get('category-new'));
$em = $this->getDoctrine()->getManager();
$em->persist($categoryName);
$em->flush();
$success = ' Category '.$categoryName->getName().$i.' was created';
}
else
{
$success = 'Test name can not be empty';
}
}
return $this->redirect($this->generateUrl('test.new'));
}