我正在使用symfony 3.0.5。 我有“收集”类型字段的表格。当我打印收集表格时,我只会得到标签。
我的代码: UserType.php
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname')
->add('lastname')
->add('username')
->add('password')
->add('address')
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User'
));
}
}
ClientType.php
class ClientType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('money')
->add('user',CollectionType::class,array(
'entry_type' => UserType::class
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Client'
));
}
Client.php
class Client
{
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="client")
* @ORM\JoinColumn(name="id", referencedColumnName="id", onDelete="CASCADE")
*/
private $user;
...//set,get:money,id,...
}
user.php的
class User
{
/**
* @ORM\OneToMany(targetEntity="Client", mappedBy="user")
*/
private $client;
public function __construct()
{
$this->client = new ArrayCollection();
}
....//set get: name,id,lastname...
}
ClientController.php
public function createAction(Request $request)
{
$client = new Client();
$user = new User();
$form = $this->createForm(ClientType::class,$client)
->add('submit',ButtonType::class);
....
}
create.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<h2 class="page-header">Client</h2>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
答案 0 :(得分:0)
你可以试试这个:
{{ form_start(form) }}
{{ form_row(form.money) }}
{{ form_row(form.user.firstname) }}
{{ form_row(form.user.lastname) }}
{{ form_end(form) }}
[编辑]
当你使用collectionType(是一个错误?)时,你需要循环用户,试试:
{{ form_start(form) }}
{{ form_row(form.money) }}
{% for user in form.user %}
{{ form_row(user.firstname) }}
{{ form_row(user.lastname) }}
{% endfor %}
{{ form_end(form) }}
但我认为你的实体配置错误,或许可以看一下:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
// ...
$builder->add('user', EntityType::class, array(
'class' => 'AppBundle:User',
));