|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldGroup; |
|
6
|
|
|
use SilverStripe\Forms\LiteralField; |
|
7
|
|
|
use SilverStripe\Forms\NumericField; |
|
8
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* EditableNumericField |
|
12
|
|
|
* |
|
13
|
|
|
* This control represents a user-defined numeric field in a user defined form |
|
14
|
|
|
* |
|
15
|
|
|
* @package userforms |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
class EditableNumericField extends EditableFormField |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
private static $singular_name = 'Numeric Field'; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
private static $plural_name = 'Numeric Fields'; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
private static $has_placeholder = true; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
private static $db = [ |
|
|
|
|
|
|
28
|
|
|
'MinValue' => 'Int', |
|
29
|
|
|
'MaxValue' => 'Int' |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
private static $table_name = 'EditableNumericField'; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function getSetsOwnError() |
|
35
|
|
|
{ |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return NumericField |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getFormField() |
|
43
|
|
|
{ |
|
44
|
|
|
$field = NumericField::create($this->Name, $this->Title ?: false, $this->Default) |
|
45
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder') |
|
46
|
|
|
->setTemplate(EditableFormField::class) |
|
47
|
|
|
->addExtraClass('number'); |
|
48
|
|
|
|
|
49
|
|
|
$this->doUpdateFormField($field); |
|
50
|
|
|
|
|
51
|
|
|
return $field; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getFieldValidationOptions() |
|
55
|
|
|
{ |
|
56
|
|
|
$fields = parent::getFieldValidationOptions(); |
|
57
|
|
|
$fields->push(FieldGroup::create( |
|
58
|
|
|
_t(__CLASS__.'.RANGE', 'Allowed numeric range'), |
|
59
|
|
|
[ |
|
60
|
|
|
NumericField::create('MinValue', false), |
|
61
|
|
|
LiteralField::create('RangeValue', _t(__CLASS__.'.RANGE_TO', 'to')), |
|
62
|
|
|
NumericField::create('MaxValue', false) |
|
63
|
|
|
] |
|
64
|
|
|
)); |
|
65
|
|
|
return $fields; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Updates a formfield with the additional metadata specified by this field |
|
70
|
|
|
* |
|
71
|
|
|
* @param FormField $field |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
|
|
protected function updateFormField($field) |
|
74
|
|
|
{ |
|
75
|
|
|
parent::updateFormField($field); |
|
76
|
|
|
|
|
77
|
|
|
if ($this->MinValue) { |
|
|
|
|
|
|
78
|
|
|
$field->setAttribute('data-rule-min', $this->MinValue); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($this->MaxValue) { |
|
|
|
|
|
|
82
|
|
|
$field->setAttribute('data-rule-max', $this->MaxValue); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
public function validate() |
|
86
|
|
|
{ |
|
87
|
|
|
$result = parent::validate(); |
|
88
|
|
|
if ($this->MinValue > $this->MaxValue) { |
|
|
|
|
|
|
89
|
|
|
$result->addError("Minimum length should be less than the Maximum length."); |
|
90
|
|
|
} |
|
91
|
|
|
return $result; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|