EditableFieldGroup.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\TextField;
  14. use Symfony\Component\Form\RecursiveFieldIterator;
  15. use Symfony\Component\Form\FieldInterface;
  16. /**
  17. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  18. */
  19. class EditableFieldGroup extends Form
  20. {
  21. /**
  22. * @inheritDoc
  23. */
  24. public function __construct($key, array $options = array())
  25. {
  26. $this->add(new CheckboxField('_delete', array(
  27. 'required' => false
  28. )));
  29. parent::__construct($key, $options);
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. protected function readObject(&$objectOrArray)
  35. {
  36. $iterator = new RecursiveFieldIterator($this);
  37. $iterator = new \RecursiveIteratorIterator($iterator);
  38. foreach ($iterator as $field) {
  39. if($field->getKey() == '_delete') {
  40. continue;
  41. }
  42. $field->readProperty($objectOrArray);
  43. }
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. protected function writeObject(&$objectOrArray)
  49. {
  50. $iterator = new RecursiveFieldIterator($this);
  51. $iterator = new \RecursiveIteratorIterator($iterator);
  52. foreach ($iterator as $field) {
  53. if($field->getKey() == '_delete') {
  54. continue;
  55. }
  56. $field->writeProperty($objectOrArray);
  57. }
  58. }
  59. }