PercentField.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. use Symfony\Component\Form\ValueTransformer\PercentToLocalizedStringTransformer;
  12. /**
  13. * A localized field for entering percentage values.
  14. *
  15. * The percentage is always rendered in its large format (e.g. 75, not .75).
  16. *
  17. * Available options:
  18. *
  19. * * percent_type: How the source number is stored on the object
  20. * * self::FRACTIONAL (e.g. stored as .75)
  21. * * self::INTEGER (e.g. stored as 75)
  22. *
  23. * By default, the precision option is set to 0, meaning that decimal integer
  24. * values will be rounded using the method specified in the rounding-mode
  25. * option.
  26. *
  27. * @see Symfony\Component\Form\NumberField
  28. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  29. */
  30. class PercentField extends NumberField
  31. {
  32. const FRACTIONAL = 'fractional';
  33. const INTEGER = 'integer';
  34. /**
  35. * {@inheritDoc}
  36. */
  37. protected function configure()
  38. {
  39. $this->addOption('precision', 0);
  40. $this->addOption('percent_type', self::FRACTIONAL);
  41. parent::configure();
  42. $this->setValueTransformer(new PercentToLocalizedStringTransformer(array(
  43. 'precision' => $this->getOption('precision'),
  44. 'type' => $this->getOption('percent_type'),
  45. )));
  46. }
  47. }