EditableCollectionField.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Sonata\BaseApplicationBundle\Form;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. use Symfony\Component\Form\Form;
  12. use Symfony\Component\Form\CheckboxField;
  13. use Symfony\Component\Form\FieldInterface;
  14. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  15. /**
  16. * @author Thomas Rabaix <thomas.rabaix@sonata-project.com>
  17. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  18. */
  19. class EditableCollectionField extends Form
  20. {
  21. /**
  22. * The prototype for the inner fields
  23. * @var FieldInterface
  24. */
  25. protected $prototype;
  26. /**
  27. * Remembers which fields were removed upon binding
  28. * @var array
  29. */
  30. protected $removedFields = array();
  31. /**
  32. * Repeats the given field twice to verify the user's input
  33. *
  34. * @param FieldInterface $innerField
  35. */
  36. public function __construct(FieldInterface $innerField, array $options = array())
  37. {
  38. $this->prototype = $innerField;
  39. parent::__construct($innerField->getKey(), $options);
  40. }
  41. public function submit($taintedData)
  42. {
  43. $this->removedFields = array();
  44. if (null === $taintedData) {
  45. $taintedData = array();
  46. }
  47. foreach ($this as $name => $field) {
  48. if (!isset($taintedData[$name]) || array_key_exists('_delete', $taintedData[$name])) {
  49. $this->remove($name);
  50. $this->removedFields[] = $name;
  51. }
  52. }
  53. // add new element (+ icon)
  54. foreach ($taintedData as $name => $value) {
  55. if (!isset($this[$name])) {
  56. $this->addField($name, $name);
  57. }
  58. }
  59. parent::submit($taintedData);
  60. }
  61. /**
  62. * Add a new element to the collection
  63. *
  64. * @param string $key
  65. * @param string $propertyPath
  66. */
  67. public function addField($key, $propertyPath)
  68. {
  69. $this->add($this->newfield($key, $propertyPath));
  70. }
  71. /**
  72. *
  73. * @return the FieldGroup prototype used to generate the collection
  74. */
  75. public function getPrototype()
  76. {
  77. return $this->prototype;
  78. }
  79. protected function newField($key, $propertyPath)
  80. {
  81. $field = clone $this->prototype;
  82. $field->setKey($key);
  83. $field->setPropertyPath(null === $propertyPath ? null : '['.$propertyPath.']');
  84. return $field;
  85. }
  86. public function setData($collection)
  87. {
  88. if (!is_array($collection) && !$collection instanceof \Traversable) {
  89. throw new UnexpectedTypeException($collection, 'array or \Traversable');
  90. }
  91. foreach ($collection as $name => $value) {
  92. $this->add($this->newField($name, $name));
  93. }
  94. parent::setData($collection);
  95. }
  96. protected function writeObject(&$objectOrArray)
  97. {
  98. parent::writeObject($objectOrArray);
  99. foreach ($this->removedFields as $name) {
  100. unset($objectOrArray[$name]);
  101. }
  102. }
  103. }