1 | <?php |
||
2 | |||
3 | namespace SilverCommerce\QuantityField\Forms; |
||
4 | |||
5 | use SilverStripe\Forms\NumericField; |
||
6 | |||
7 | /** |
||
8 | * Text input field with validation for numeric values. |
||
9 | * |
||
10 | * @package quantityfield |
||
11 | */ |
||
12 | class QuantityField extends NumericField |
||
13 | { |
||
14 | /** |
||
15 | * Construct this field (we override the default to set a default value) |
||
16 | * |
||
17 | * @param [type] $name |
||
18 | * @param [type] $title |
||
19 | * @param string $value |
||
20 | * @param [type] $maxLength |
||
21 | * @param [type] $form |
||
22 | */ |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
23 | public function __construct($name, $title = null, $value = 1, $maxLength = null, $form = null) |
||
24 | { |
||
25 | parent::__construct($name, $title, $value); |
||
26 | } |
||
27 | |||
28 | public function Type() |
||
29 | { |
||
30 | return 'quantity numeric text'; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * PHP Validation |
||
35 | * |
||
36 | * @return boolean |
||
37 | * @throws Exception |
||
38 | **/ |
||
39 | public function validate($validator) |
||
40 | { |
||
41 | $value = $this->Value() + 0; |
||
42 | |||
43 | if(is_int($value)) { |
||
0 ignored issues
–
show
|
|||
44 | return true; |
||
45 | } |
||
46 | |||
47 | $validator->validationError( |
||
48 | $this->name, |
||
49 | _t( |
||
50 | 'Checkout.VALIDATION', '{value} is not a valid number, only whole numbers can be accepted for this field', |
||
51 | array('value' => $value) |
||
52 | ), |
||
53 | "validation" |
||
54 | ); |
||
55 | |||
56 | return false; |
||
57 | } |
||
58 | |||
59 | public function dataValue() |
||
60 | { |
||
61 | $value = $this->Value(); |
||
62 | $value = (is_numeric($value)) ? $value : 0; |
||
63 | |||
64 | return $this->cast($value); |
||
65 | } |
||
66 | } |
||
67 |