Created
February 21, 2013 18:34
-
-
Save simshaun/5006957 to your computer and use it in GitHub Desktop.
Example of a re-using a FormType in other forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% import 'S2FooBundle:Form:macro.html.twig' as macro %} | |
{% form_theme fooAddressForm 'S2FooBundle:Form:theme.html.twig' %} | |
<form id="foo-address-form" method="post" action="{{ path('s2foo.foo_edit', {id: fooId}) }}"> | |
{{ form_errors(fooAddressForm) }} | |
{% set field='name' %} | |
<div class="row"> | |
<div class="three columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_label(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
<div class="nine columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_widget(attribute(fooAddressForm.foo_address, field), {'attr': {'class': 'focus-first'}}) }} | |
{{ form_errors(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
</div> | |
{% set field='address' %} | |
<div class="row"> | |
<div class="three columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_label(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
<div class="nine columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_widget(attribute(fooAddressForm.foo_address, field)) }} | |
{{ form_errors(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
</div> | |
{% set field='city' %} | |
<div class="row"> | |
<div class="three columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_label(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
<div class="nine columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_widget(attribute(fooAddressForm.foo_address, field)) }} | |
{{ form_errors(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
</div> | |
{% set field='state' %} | |
<div class="row"> | |
<div class="three columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_label(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
<div class="nine columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_widget(attribute(fooAddressForm.foo_address, field)) }} | |
{{ form_errors(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
</div> | |
{% set field='zip' %} | |
<div class="row"> | |
<div class="three columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_label(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
<div class="nine columns {{ macro.hasErrors(attribute(fooAddressForm.foo_address, field)) }}" data-field={{ field }}"> | |
{{ form_widget(attribute(fooAddressForm.foo_address, field)) }} | |
{{ form_errors(attribute(fooAddressForm.foo_address, field)) }} | |
</div> | |
</div> | |
{% if display_form_end is defined and display_form_end %} | |
<div class="row"> | |
<input type="submit" name="submit" value="Save" class="button"> | |
</div> | |
{% endif %} | |
{{ form_rest(fooAddressForm) }} | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace S2\FooBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use S2\FooBundle\Form\Type\PartialAddressType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
class FooAddressType extends AbstractType | |
{ | |
protected $partialAddressForm; | |
public function __construct(PartialAddressType $partialAddressForm) | |
{ | |
$this->partialAddressForm = $partialAddressForm; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('foo_address', $this->partialAddressForm); | |
} | |
public function getName() | |
{ | |
return 'foo_address'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace S2\FooBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use S2\FooBundle\Entity\StateManager; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class PartialAddressType extends AbstractType | |
{ | |
private $addressClass; | |
/** | |
* @var StateManager | |
*/ | |
protected $stateManger; | |
public function __construct($addressClass, StateManager $stateManager) | |
{ | |
$this->addressClass = $addressClass; | |
$this->stateManger = $stateManager; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('name', 'text', array( | |
'required' => false, | |
'label_attr' => array( | |
'class' => 'right inline', | |
), | |
)); | |
$builder->add('address', 'text', array( | |
'required' => false, | |
'label_attr' => array( | |
'class' => 'right inline', | |
), | |
)); | |
$builder->add('city', 'text', array( | |
'required' => false, | |
'label_attr' => array( | |
'class' => 'right inline', | |
), | |
)); | |
$builder->add('state', 'choice', array( | |
'choices' => $this->stateManger->findAllStates()->toArray(), | |
'empty_value' => '- Choose an option -', | |
'empty_data' => null, | |
'required' => false, | |
'label_attr' => array( | |
'class' => 'right inline', | |
), | |
)); | |
$builder->add('zip', 'number', array( | |
'required' => false, | |
'label_attr' => array( | |
'class' => 'right inline', | |
), | |
)); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => $this->addressClass, | |
'cascade_validation' => true, | |
)); | |
} | |
public function getName() | |
{ | |
return 'partial_address'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment