EditableCollectionField.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Bundle\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\FieldGroup;
  12. use Symfony\Component\Form\CheckboxField;
  13. use Symfony\Component\Form\FieldInterface;
  14. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  15. /**
  16. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  17. */
  18. class EditableCollectionField extends FieldGroup
  19. {
  20. /**
  21. * The prototype for the inner fields
  22. * @var FieldInterface
  23. */
  24. protected $prototype;
  25. /**
  26. * Remembers which fields were removed upon binding
  27. * @var array
  28. */
  29. protected $removedFields = array();
  30. /**
  31. * Repeats the given field twice to verify the user's input
  32. *
  33. * @param FieldInterface $innerField
  34. */
  35. public function __construct(FieldInterface $innerField, array $options = array())
  36. {
  37. $innerField->add(new CheckboxField('_delete'));
  38. $this->prototype = $innerField;
  39. parent::__construct($innerField->getKey(), $options);
  40. }
  41. public function bind($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. foreach ($taintedData as $name => $value) {
  54. if (!isset($this[$name])) {
  55. $this->add($this->newField($name, $name));
  56. }
  57. }
  58. parent::bind($taintedData);
  59. }
  60. /**
  61. * Add a new element to the collection
  62. *
  63. * @param string $key
  64. * @param string $propertyPath
  65. */
  66. public function addField($key, $propertyPath)
  67. {
  68. $this->add($this->newfield($key, $propertyPath));
  69. }
  70. /**
  71. *
  72. * @return the FieldGroup prototype used to generate the collection
  73. */
  74. public function getPrototype()
  75. {
  76. return $this->prototype;
  77. }
  78. protected function newField($key, $propertyPath)
  79. {
  80. $field = clone $this->prototype;
  81. $field->setKey($key);
  82. $field->setPropertyPath(null === $propertyPath ? null : '['.$propertyPath.']');
  83. return $field;
  84. }
  85. public function setData($collection)
  86. {
  87. if (!is_array($collection) && !$collection instanceof \Traversable) {
  88. throw new UnexpectedTypeException($collection, 'array or \Traversable');
  89. }
  90. foreach ($collection as $name => $value) {
  91. $this->add($this->newField($name, $name));
  92. }
  93. parent::setData($collection);
  94. }
  95. protected function updateObject(&$objectOrArray)
  96. {
  97. parent::updateObject($objectOrArray);
  98. foreach ($this->removedFields as $name) {
  99. unset($objectOrArray[$name]);
  100. }
  101. }
  102. }