Currency.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. class Currency extends Text
  4. {
  5. public static $js = '@jquery.inputmask';
  6. public static $css = '@jquery.inputmask';
  7. /**
  8. * @var string
  9. */
  10. protected $symbol = '$';
  11. /**
  12. * @see https://github.com/RobinHerbots/Inputmask#options
  13. *
  14. * @var array
  15. */
  16. protected $options = [
  17. 'alias' => 'currency',
  18. 'radixPoint' => '.',
  19. 'prefix' => '',
  20. 'removeMaskOnSubmit' => true,
  21. ];
  22. /**
  23. * Set symbol for currency field.
  24. *
  25. * @param string $symbol
  26. *
  27. * @return $this
  28. */
  29. public function symbol($symbol)
  30. {
  31. $this->symbol = $symbol;
  32. return $this;
  33. }
  34. /**
  35. * Set digits for input number.
  36. *
  37. * @param int $digits
  38. *
  39. * @return $this
  40. */
  41. public function digits($digits)
  42. {
  43. return $this->options(compact('digits'));
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function prepareInputValue($value)
  49. {
  50. return (float) $value;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function render()
  56. {
  57. $this->inputmask($this->options);
  58. $this->prepend($this->symbol)
  59. ->defaultAttribute('style', 'width: 200px');
  60. return parent::render();
  61. }
  62. }