Debian Codegraph @

includes/htmlform/fields/HTMLFloatField.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\HTMLForm\Field;
4
5 /**
6 * A field that will contain a numeric value
7 *
8 * @stable to extend
9 */
10 class HTMLFloatField extends HTMLTextField {
11 public function getSize() {
12 return $this->mParams['size'] ?? 20;
13 }
14
15 public function validate( $value, $alldata ) {
16 $p = parent::validate( $value, $alldata );
17
18 if ( $p !== true ) {
19 return $p;
20 }
21
22 $value = trim( $value ?? '' );
23 if ( $value === '' ) {
24 return true;
25 }
26
27 # https://www.w3.org/TR/html5/infrastructure.html#floating-point-numbers
28 # with the addition that a leading '+' sign is ok.
29 if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
30 return $this->msg( 'htmlform-float-invalid' );
31 }
32
33 # The "int" part of these message names is rather confusing.
34 # They make equal sense for all numbers.
35 if ( isset( $this->mParams['min'] ) ) {
36 $min = $this->mParams['min'];
37
38 if ( $min > $value ) {
39 return $this->msg( 'htmlform-int-toolow', $min );
40 }
41 }
42
43 if ( isset( $this->mParams['max'] ) ) {
44 $max = $this->mParams['max'];
45
46 if ( $max < $value ) {
47 return $this->msg( 'htmlform-int-toohigh', $max );
48 }
49 }
50
51 return true;
52 }
53
54 /**
55 * @inheritDoc
56 * @stable to override
57 */
58 protected function getInputWidget( $params ) {
59 return new \OOUI\NumberInputWidget( $params );
60 }
61 }
62
63 /** @deprecated class alias since 1.42 */
64 class_alias( HTMLFloatField::class, 'HTMLFloatField' );