EditableCollectionField.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. $innerField->add(new CheckboxField('_delete', array(
  39. 'required' => false
  40. )));
  41. $this->prototype = $innerField;
  42. parent::__construct($innerField->getKey(), $options);
  43. }
  44. public function submit($taintedData)
  45. {
  46. $this->removedFields = array();
  47. if (null === $taintedData) {
  48. $taintedData = array();
  49. }
  50. foreach ($this as $name => $field) {
  51. if (!isset($taintedData[$name]) || array_key_exists('_delete', $taintedData[$name])) {
  52. $this->remove($name);
  53. $this->removedFields[] = $name;
  54. }
  55. }
  56. foreach ($taintedData as $name => $value) {
  57. if (!isset($this[$name])) {
  58. $this->add($this->newField($name, $name));
  59. }
  60. }
  61. parent::submit($taintedData);
  62. }
  63. /**
  64. * Add a new element to the collection
  65. *
  66. * @param string $key
  67. * @param string $propertyPath
  68. */
  69. public function addField($key, $propertyPath)
  70. {
  71. $this->add($this->newfield($key, $propertyPath));
  72. }
  73. /**
  74. *
  75. * @return the FieldGroup prototype used to generate the collection
  76. */
  77. public function getPrototype()
  78. {
  79. return $this->prototype;
  80. }
  81. protected function newField($key, $propertyPath)
  82. {
  83. $field = clone $this->prototype;
  84. $field->setKey($key);
  85. $field->setPropertyPath(null === $propertyPath ? null : '['.$propertyPath.']');
  86. return $field;
  87. }
  88. public function setData($collection)
  89. {
  90. if (!is_array($collection) && !$collection instanceof \Traversable) {
  91. throw new UnexpectedTypeException($collection, 'array or \Traversable');
  92. }
  93. foreach ($collection as $name => $value) {
  94. $this->add($this->newField($name, $name));
  95. }
  96. parent::setData($collection);
  97. }
  98. protected function updateObject(&$objectOrArray)
  99. {
  100. parent::updateObject($objectOrArray);
  101. foreach ($this->removedFields as $name) {
  102. unset($objectOrArray[$name]);
  103. }
  104. }
  105. }