Skip to content

Instantly share code, notes, and snippets.

@yethee
Created August 17, 2011 09:02
Show Gist options
  • Save yethee/1151134 to your computer and use it in GitHub Desktop.
Save yethee/1151134 to your computer and use it in GitHub Desktop.
Custom template for collection form
<?php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PersonFormType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add('addresses', 'collection', array(
'type' => new AddressFormType(),
'allow_add' => true,
));
}
public function getName()
{
return 'form_person';
}
}
<?php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class AddressFormType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('city')
->add('street');
}
public function getName()
{
return 'form_address';
}
}
{% form_theme form _self %}
<form action="{{ app.request.uri }}" method="post">
{{ form_row(form.name, {'label': 'Name:'}) }}
<h3>Addresses</h3>
{{ form_widget(form.addresses) }}
<input type="submit" />
</form>
{% block form_address_row %}
{{ form_row(form.city, {'label': 'City:'}) }}
{{ form_row(form.street, {'label': 'Street:'}) }}
{% endblock %}
{% form_theme form _self %}
<form action="{{ app.request.uri }}" method="post">
{{ form_row(form.name, {'label': 'Name:'}) }}
<h3>Addresses</h3>
<div data-prototype="{{ form_row(form.addresses.get('prototype'))|e }}">
{% for child in form.addresses %}
{{ form_row(child) }}
{% endfor %}
</div>
<input type="submit" />
</form>
{% block form_address_row %}
{{ form_row(form.city, {'label': 'City:'}) }}
{{ form_row(form.street, {'label': 'Street:'}) }}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment