FieldConfig.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Config;
  11. use Symfony\Component\Form\Field;
  12. use Symfony\Component\Form\FieldInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\Form\Renderer\DefaultRenderer;
  15. use Symfony\Component\Form\Renderer\Theme\ThemeInterface;
  16. use Symfony\Component\Form\Renderer\Plugin\FieldPlugin;
  17. use Symfony\Component\Form\EventListener\TrimListener;
  18. use Symfony\Component\EventDispatcher\EventDispatcher;
  19. class FieldConfig extends AbstractFieldConfig
  20. {
  21. private $theme;
  22. public function __construct(FormFactoryInterface $factory, ThemeInterface $theme)
  23. {
  24. parent::__construct($factory);
  25. $this->theme = $theme;
  26. }
  27. public function configure(FieldInterface $field, array $options)
  28. {
  29. $field->setPropertyPath($options['property_path'] === false
  30. ? $field->getName()
  31. : $options['property_path'])
  32. ->setRequired($options['required'])
  33. ->setDisabled($options['disabled'])
  34. ->setValueTransformer($options['value_transformer'])
  35. ->setNormalizationTransformer($options['normalization_transformer'])
  36. ->setData($options['data'])
  37. ->setRenderer(new DefaultRenderer($field, $this->theme, $options['template']))
  38. ->addRendererPlugin(new FieldPlugin());
  39. if ($options['trim']) {
  40. $field->addEventSubscriber(new TrimListener());
  41. }
  42. }
  43. public function getDefaultOptions(array $options)
  44. {
  45. return array(
  46. 'template' => 'text',
  47. 'data' => null,
  48. 'property_path' => false,
  49. 'trim' => true,
  50. 'required' => true,
  51. 'disabled' => false,
  52. 'value_transformer' => null,
  53. 'normalization_transformer' => null,
  54. );
  55. }
  56. public function createInstance($name)
  57. {
  58. return new Field($name, new EventDispatcher());
  59. }
  60. public function getParent(array $options)
  61. {
  62. return null;
  63. }
  64. public function getIdentifier()
  65. {
  66. return 'field';
  67. }
  68. }