NumberField.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\NumberToLocalizedStringTransformer;
  12. /**
  13. * A localized field for entering numbers.
  14. *
  15. * Available options:
  16. *
  17. * * precision: The number of digits to allow when rounding. Default
  18. * is locale-specific.
  19. * * grouping:
  20. * * rounding-mode: The method to use to round to get to the needed level
  21. * of precision. Options include:
  22. * * NumberToLocalizedStringTransformer::ROUND_FLOOR
  23. * * NumberToLocalizedStringTransformer::ROUND_DOWN
  24. * * NumberToLocalizedStringTransformer::ROUND_HALFDOWN
  25. * * NumberToLocalizedStringTransformer::ROUND_HALFUP (default)
  26. * * NumberToLocalizedStringTransformer::ROUND_UP
  27. * * NumberToLocalizedStringTransformer::ROUND_CEILING
  28. *
  29. * @see \NumberFormatter
  30. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  31. */
  32. class NumberField extends Field
  33. {
  34. /**
  35. * {@inheritDoc}
  36. */
  37. protected function configure()
  38. {
  39. // default precision is locale specific (usually around 3)
  40. $this->addOption('precision');
  41. $this->addOption('grouping', false);
  42. $this->addOption('rounding-mode', NumberToLocalizedStringTransformer::ROUND_HALFUP);
  43. parent::configure();
  44. $this->setValueTransformer(new NumberToLocalizedStringTransformer(array(
  45. 'precision' => $this->getOption('precision'),
  46. 'grouping' => $this->getOption('grouping'),
  47. 'rounding-mode' => $this->getOption('rounding-mode'),
  48. )));
  49. }
  50. }