0

我需要帮助我创建的表单,我在一个 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'));
} 
4

2 回答 2

1

不确定我是否理解正确。您希望输入名称为“category-new”且具有不同值的输入。如果是这样,您的表单视图有问题:

Category *<input type="text" name="category-new" ><br>

name 应该在末尾有括号 name="category-new[]" 或 name="category-new[1]" 用于类别 1,name="category-new[2]" 用于类别 2,依此类推。

于 2012-10-30T13:37:50.160 回答
0

模板:

    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>

有趣的是,您的控制器采用名称为 category-new 的数组

/**
 * @Route("/add/test")
 * 
 * @return array
 */
public function createAction()
{
    $success = 0;
    $name = $this->getRequest()->get('name');

    if (!empty($name)) {
        $test = new Test();
        $test->setName($name);

        $em = $this->getDoctrine()->getManager();
        $em->persist($test);
        $em->flush();

        $success = 'Test ' . $test->getName() . ' was created';
    } else {
        $success = 'Test name can not be empty';
    }

    $categoryList = array_map('trim', $this->getRequest()->get('category-new'));

    foreach ($categoryList as $category) {
        if (!empty($category)) {
            $categoryName = new Category();
            $categoryName->setName($category);

            $em = $this->getDoctrine()->getManager();
            $em->persist($categoryName);
            $em->flush();

            $success = ' Category ' . $category . ' was created';

        } else {
            $success = 'Test category-new may not be empty';
        }
    }
    return $this->redirect($this->generateUrl('test_create'));
}

一些单词

Route("/add/test", requirements={"name" = "\s+"}, name="test.create")

您不需要此操作的名称为您的 project_bundlename_controller_action {project}_{bundlename}_test_create 我不知道您的项目名称和捆绑名称 - 我认为这对您来说并不困难。还有一些代码:)

于 2012-10-30T14:54:53.250 回答