-
-
Save zdenekdrahos/1fd7db0ccfc75554aeef to your computer and use it in GitHub Desktop.
Cleaner Symfony forms
This file contains hidden or 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 AppBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
abstract class CleanFormType extends AbstractType | |
{ | |
private $formName; | |
public function __construct($formName) | |
{ | |
$this->formName = $formName; | |
} | |
public function getName() | |
{ | |
return $this->formName; | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults( | |
$this->getDefaults() | |
); | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$this->loadOptions($options); | |
$this->build($builder); | |
} | |
protected function getDefaults() | |
{ | |
return array(); | |
} | |
/** @SuppressWarnings("unused") */ | |
protected function loadOptions(array $options) | |
{ | |
} | |
abstract protected function build(FormBuilderInterface $builder); | |
} |
This file contains hidden or 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
services: | |
acme_demo.form.type.task: | |
class: AppBundle\Form\Type\TaskType | |
arguments: | |
- "task" | |
tags: | |
- { name: form.type, alias: task } |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment