Symfony2 - 如何定义表单和控制器以一次动态创建实体的多个对象?

时间:2016-02-17 17:09:55

标签: forms symfony

Symfony2 2.7

我环顾四周,但我找不到一种简洁,标准的定义方式:

(*)一个表单(formType)以及让用户动态创建任意数量的对象所需的控制器。

我看到herehere,但我无法重现他们的解决方案。

问题是:有没有标准的方法(*)?

注意:This似乎没有帮助,因为它只创建一个唯一的Task对象,即使它允许用户创建(动态)多个Tag对象。

部分解决方案 以下工作正常:用户可以添加任意数量的对象。

我需要检查一下是否可以以某种方式清理代码。 如果你有办法改进它,请发表评论。

最终,我希望有一个Job 1:M任务1:M标签的情况,用一个表格立即创建多个作业,每个作业有很多任务,每个任务有很多标签。我需要检查我是否可以为相关的1:M实体嵌入表单。请继续关注!

控制器

/**
 * Creates a new Job entity.
 *
 * @Route("/", name="Myname_Job_create")
 * @Method("POST")
 * @Template("MynameBlogBundle:Job:new.html.twig")
 */
public function createAction(Request $request)
{
    ////var_dump($request); die('here'); // this helped a lot to understand
    $postData = $request->request->get('form');
    $entity = array();
    foreach($postData['jobs'] as $key => $obj){$entity[$key]= new Job();}
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
       try {

            $em = $this->getDoctrine()->getEntityManager();
            foreach($entity as $ent){ $em->persist($ent); }
            $em->flush();  

        } catch (\Doctrine\DBAL\DBALException $e) {
            die($e->getMessage());
    }
        return $this->redirect($this->generateUrl('Myname_Job'));
    }
    return array(
        'info' =>   $postData,
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

/**
 * Creates a form to create a Job entity.
 *
 * @param Job $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(Array $jobs)
{
   $form = $this->createFormBuilder(array('jobs'=>$jobs))
            ->setAction($this->generateUrl('Myname_Job_create'))
            ->add('jobs','collection',array(
                'required'       => true,
                'allow_add'      => true,
                'allow_delete'  => true,
                'type'           => new JobType(),
           ))
            ->add('submit', 'submit',array('label' => 'Create'))
            ->getForm()
        ;

    return $form;
}

/**
 * Displays a form to create a new Job entity.
 *
 * @Route("/new", name="Myname_Job_new")
 * @Method("GET")
 * @Template()
 */
public function newAction()
{


    $jobs = array(0 => new Job());
    $form = $this->createCreateForm($jobs);

    return array(
        'entity' => $jobs,
        'form'   => $form->createView(),
    );
}

JobType

class JobType extends AbstractType
{
  /**
  * @param FormBuilderInterface $builder
  * @param array $options
  */

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', 'text', array(
            'label' => 'Write Title   ',
            'required' => true
        ))
        ->add('description','text',array(
            'label'     => 'Write Descr.  ',

        )) 
    ;
}
....
}

新的.twig模板

{% extends '::base.html.twig' %}

{% block javascripts %}
     {{ parent() }}

        <script src="{{ asset('bundles/mynameblog/js/JQuery/jquery-2.1.0.js') }}" type="text/javascript">
</script>
<script>
var $collectionHolder;

// setup an "add aJobs" link
var $addJobLink = $('<a href="#" class="add_Job_link">Add Jobs</a>');
var $newLinkLi = $('<li></li>').append($addJobLink);

      function addJobForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');

// get the new index
var index = $collectionHolder.data('index');

// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);

// increase the index with one for the next item
$collectionHolder.data('index', index + 1);

// Display the form in the page in an li, before the "Add a Jobs" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
    }

jQuery(document).ready(function() {
// Get the ul that holds the collection of Jobs
$collectionHolder = $('ul.jobs');

// add the "add Jobs" anchor and li to the Jobs ul
$collectionHolder.append($newLinkLi);

// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);

$addJobLink.on('click', function(e) {
    // prevent the link from creating a "#" on the URL
    e.preventDefault();

    // add a newJobs form (see next code block)
    addJobForm($collectionHolder, $newLinkLi);
});
});
</script>
{% endblock %}
{% block main -%}  
 <h1>Job creation</h1>

{{ form_start(form) }}

<ul class="jobs" data-prototype="{{ form_widget(form.jobs.vars.prototype)|e }}">

{# render the job-s only two fields: title description #}

{% for f in form.jobs %}
           <li>   {{ form_row(f) }} </li>
            {% endfor %}
 </ul>
 {{ form_end(form) }}
 {% endblock %}

1 个答案:

答案 0 :(得分:0)

建议:使用一个客户端操作添加多个对象的最佳方式:

  • 为每个对象创建一个表单(只需用JS复制)
  • 使用JS,听取提交按钮并使用不同的ajax请求发送每个表单。

所以很简单...... 无需复杂的表格来管理接收已发布请求的控制器中的多个实体。

但是,要做你想做的事情:

这是一种做你想做的事的方法。 我只强调了如何在服务器端调整表单。 我无法测试你的JS,在这里我假设你在表单中添加[title_1,description_1,title_2,description_2,...]等字段。

你也应该适应:

  • 如何创建实体
  • 如何验证它们

我建议您使用“objects_count”选项调整表单: (请注意,您应该处理没有类的表单(不应在configureOptions中定义data_class))

class JobType extends AbstractType
{
  /**
  * @param FormBuilderInterface $builder
  * @param array $options
  */

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // This is called once you posted your form to recreate the JS generated
        // fiels on the server side. Otherwise, these fiels are ignorated
        if($options['objects_count']>0){
            for($i=0;$i<$options['objects_count'];$i++){
                $builder
                    ->add('title_'.$i, TextType::class)
                    ->add('description_'.$i, TextType::class) 
                ;
            }
        }

        // This is called to render the first form
        else{
            $builder
                ->add('title', TextType::class)
                ->add('description', TextType::class) 
            ;
        }
    }

    /**
    * @param OptionsResolver $resolver
    */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array('objects_count'=>0));
    }
}

然后,要处理此表单,您必须获取已过帐字段的计数:

你的控制器:

/**
 * Creates a new Job entity.
 *
 * @Route("/", name="Myname_Job_create")
 * @Method("POST")
 * @Template("MynameBlogBundle:Job:new.html.twig")
 */
public function createAction(Request $request)
{
    $i = 1;
    $defaults = array();
    while($request->request->has('title_'.$i)){
        $defaults += array('title_'.$i=>"",'description_'.$i=>"");
        $i++;
    }


    $objects_count = $i-1;
    if($objects_count === 0){
        $defaults = array('title'=>"",'description'=>"");
    }


    $form = $this->$defaults(JobType::class,$defaults,array('objects_count'=>$objects_count));
    $form->handleRequest($request);

    // ...
}