HTMLFloatField.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * A field that will contain a numeric value
  4. */
  5. class HTMLFloatField extends HTMLTextField {
  6. public function getSize() {
  7. return $this->mParams['size'] ?? 20;
  8. }
  9. public function validate( $value, $alldata ) {
  10. $p = parent::validate( $value, $alldata );
  11. if ( $p !== true ) {
  12. return $p;
  13. }
  14. $value = trim( $value );
  15. # https://www.w3.org/TR/html5/infrastructure.html#floating-point-numbers
  16. # with the addition that a leading '+' sign is ok.
  17. if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
  18. return $this->msg( 'htmlform-float-invalid' );
  19. }
  20. # The "int" part of these message names is rather confusing.
  21. # They make equal sense for all numbers.
  22. if ( isset( $this->mParams['min'] ) ) {
  23. $min = $this->mParams['min'];
  24. if ( $min > $value ) {
  25. return $this->msg( 'htmlform-int-toolow', $min );
  26. }
  27. }
  28. if ( isset( $this->mParams['max'] ) ) {
  29. $max = $this->mParams['max'];
  30. if ( $max < $value ) {
  31. return $this->msg( 'htmlform-int-toohigh', $max );
  32. }
  33. }
  34. return true;
  35. }
  36. protected function getInputWidget( $params ) {
  37. return new OOUI\NumberInputWidget( $params );
  38. }
  39. }