RepeatedField.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /**
  12. * A field for repeated input of values.
  13. *
  14. * Available options:
  15. *
  16. * * first_key: The key to use for the first field.
  17. * * second_key: The key to use for the second field.
  18. *
  19. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  20. */
  21. class RepeatedField extends Form
  22. {
  23. /**
  24. * The prototype for the inner fields
  25. * @var FieldInterface
  26. */
  27. protected $prototype;
  28. /**
  29. * Repeats the given field twice to verify the user's input.
  30. *
  31. * @param FieldInterface $innerField
  32. */
  33. public function __construct(FieldInterface $innerField, array $options = array())
  34. {
  35. $this->prototype = $innerField;
  36. parent::__construct($innerField->getKey(), $options);
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. protected function configure()
  42. {
  43. $this->addOption('first_key', 'first');
  44. $this->addOption('second_key', 'second');
  45. parent::configure();
  46. $field = clone $this->prototype;
  47. $field->setKey($this->getOption('first_key'));
  48. $field->setPropertyPath($this->getOption('first_key'));
  49. $this->add($field);
  50. $field = clone $this->prototype;
  51. $field->setKey($this->getOption('second_key'));
  52. $field->setPropertyPath($this->getOption('second_key'));
  53. $this->add($field);
  54. }
  55. /**
  56. * Returns whether both entered values are equal
  57. *
  58. * @return Boolean
  59. */
  60. public function isFirstEqualToSecond()
  61. {
  62. return $this->get($this->getOption('first_key'))->getData() === $this->get($this->getOption('second_key'))->getData();
  63. }
  64. /**
  65. * Sets the values of both fields to this value
  66. *
  67. * @param mixed $data
  68. */
  69. public function setData($data)
  70. {
  71. parent::setData(array(
  72. $this->getOption('first_key') => $data,
  73. $this->getOption('second_key') => $data
  74. ));
  75. }
  76. /**
  77. * Return the value of a child field
  78. *
  79. * If the value of the first field is set, this value is returned.
  80. * Otherwise the value of the second field is returned. This way,
  81. * this field will never trigger a NotNull/NotBlank error if any of the
  82. * child fields was filled in.
  83. *
  84. * @return string The field value
  85. */
  86. public function getData()
  87. {
  88. // Return whichever data is set. This should not return NULL if any of
  89. // the fields is set, otherwise this might trigger a NotNull/NotBlank
  90. // error even though some value was set
  91. $data1 = $this->get($this->getOption('first_key'))->getData();
  92. $data2 = $this->get($this->getOption('second_key'))->getData();
  93. return $data1 ?: $data2;
  94. }
  95. }