简体   繁体   English

如何在symfony2上显示具有现有数据的填充表格

[英]Howto show filled form with existing data on symfony2

I'm trying to present a symfony2 filled form using existing data from the database. 我正在尝试使用数据库中的现有数据来呈现symfony2填充的表单。 What I want is to update data. 我想要的是更新数据。 For this I want to display the current data on the form. 为此,我想在表单上显示当前数据。

Currently I'm getting the data and filling the form, but I think this is not the best way to do it. 目前,我正在获取数据并填写表格,但是我认为这不是最好的方法。

<form>
{% for value in values %}
    <input type="text" placeholder="{{ value.name }}">
    <input type="email" placeholder="{{ value.email }}">
    ...
{% endfor %}
</form>

I know how to create a form with createFormBuilder and AbstractTypes, but not how to show or present this form filled with existing data from entities. 我知道如何使用createFormBuilder和AbstractTypes创建表单,但是不知道如何显示或呈现填充有来自实体的现有数据的表单。

Why are you building the form by hand? 您为什么要手工构建表格? You should only have {{ form(form) }} . 您只能使用{{ form(form) }} That's why everyone is asking you where do you create it, because it's the same place as where you fill it. 这就是每个人都问您在哪里创建它的原因,因为它与填充它的位置相同。 From the Form manual: 从表单手册中:

public function newAction(Request $request)
{
    // you can use a new instance of an entity or
    // get existing data from your database 
    // e.g. $task = $em->getRepository('Bundle:Task')->find(1);
    // and this data will fill your form.
    $task = new Task();
    $task->setTask('Write a blog post');
    $task->setDueDate(new \DateTime('tomorrow'));

    $form = $this->createFormBuilder($task)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->add('save', 'submit')
        ->getForm();

    return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
        'form' => $form->createView(),
    ));
}

See the call to createFormBuilder ? 看到对createFormBuilder的调用吗? That's where you input the data to your form. 那就是您在表格中输入数据的地方。 The data in the passed object / array will be used as the fill data for your form. 传递的对象/数组中的数据将用作表单的填充数据。 Now, it's up to you to provide the wanted data, whether it's an entity from the db or an array with arbitrary data. 现在,由您提供所需的数据,无论是数据库中的实体还是包含任意数据的数组。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM