CollectionField.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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;
  11. use Symfony\Component\Form\FieldInterface;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. /**
  14. * A field group that repeats the given field multiple times over a collection
  15. * specified by the property path if the field.
  16. *
  17. * Example usage:
  18. *
  19. * $form->add(new CollectionField(new TextField('emails')));
  20. *
  21. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  22. */
  23. class CollectionField extends Form
  24. {
  25. /**
  26. * Remembers which fields were removed upon submitting
  27. * @var array
  28. */
  29. protected $removedFields = array();
  30. /**
  31. * The prototype field for the collection rows
  32. * @var FieldInterface
  33. */
  34. protected $prototype;
  35. public function __construct($key, array $options = array())
  36. {
  37. // This doesn't work with addOption(), because the value of this option
  38. // needs to be accessed before Configurable::__construct() is reached
  39. // Setting all options in the constructor of the root field
  40. // is conceptually flawed
  41. if (isset($options['prototype'])) {
  42. $this->prototype = $options['prototype'];
  43. unset($options['prototype']);
  44. }
  45. parent::__construct($key, $options);
  46. }
  47. /**
  48. * Available options:
  49. *
  50. * * modifiable: If true, elements in the collection can be added
  51. * and removed by the presence of absence of the
  52. * corresponding field groups. Field groups could be
  53. * added or removed via Javascript and reflected in
  54. * the underlying collection. Default: false.
  55. */
  56. protected function configure()
  57. {
  58. $this->addOption('modifiable', false);
  59. if ($this->getOption('modifiable')) {
  60. $field = $this->newField('$$key$$', null);
  61. // TESTME
  62. $field->setRequired(false);
  63. $this->add($field);
  64. }
  65. parent::configure();
  66. }
  67. public function setData($collection)
  68. {
  69. if (null === $collection) {
  70. $collection = array();
  71. }
  72. if (!is_array($collection) && !$collection instanceof \Traversable) {
  73. throw new UnexpectedTypeException($collection, 'array or \Traversable');
  74. }
  75. foreach ($this as $name => $field) {
  76. if (!$this->getOption('modifiable') || '$$key$$' != $name) {
  77. $this->remove($name);
  78. }
  79. }
  80. foreach ($collection as $name => $value) {
  81. $this->add($this->newField($name, $name));
  82. }
  83. parent::setData($collection);
  84. }
  85. public function submit($data)
  86. {
  87. $this->removedFields = array();
  88. if (null === $data) {
  89. $data = array();
  90. }
  91. foreach ($this as $name => $field) {
  92. if (!isset($data[$name]) && $this->getOption('modifiable') && '$$key$$' != $name) {
  93. $this->remove($name);
  94. $this->removedFields[] = $name;
  95. }
  96. }
  97. foreach ($data as $name => $value) {
  98. if (!isset($this[$name]) && $this->getOption('modifiable')) {
  99. $this->add($this->newField($name, $name));
  100. }
  101. }
  102. parent::submit($data);
  103. }
  104. protected function writeObject(&$objectOrArray)
  105. {
  106. parent::writeObject($objectOrArray);
  107. foreach ($this->removedFields as $name) {
  108. unset($objectOrArray[$name]);
  109. }
  110. }
  111. protected function newField($key, $propertyPath)
  112. {
  113. if (null !== $propertyPath) {
  114. $propertyPath = '['.$propertyPath.']';
  115. }
  116. if ($this->prototype) {
  117. $field = clone $this->prototype;
  118. $field->setKey($key);
  119. $field->setPropertyPath($propertyPath);
  120. } else {
  121. $field = new TextField($key, array(
  122. 'property_path' => $propertyPath,
  123. ));
  124. }
  125. return $field;
  126. }
  127. }