Completed
Push — master ( 305cf3...b2101c )
by Robbie
22s queued 10s
created

EditableNumericField::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
22
23
    private static $plural_name = 'Numeric Fields';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
24
25
    private static $has_placeholder = true;
0 ignored issues
show
introduced by
The private property $has_placeholder is not used, and could be removed.
Loading history...
26
27
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
28
        'MinValue' => 'Int',
29
        'MaxValue' => 'Int'
30
    ];
31
32
    private static $table_name = 'EditableNumericField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
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
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ableFormField\FormField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
     */
73
    protected function updateFormField($field)
74
    {
75
        parent::updateFormField($field);
76
77
        if ($this->MinValue) {
0 ignored issues
show
Bug Best Practice introduced by
The property MinValue does not exist on SilverStripe\UserForms\M...ld\EditableNumericField. Since you implemented __get, consider adding a @property annotation.
Loading history...
78
            $field->setAttribute('data-rule-min', $this->MinValue);
79
        }
80
81
        if ($this->MaxValue) {
0 ignored issues
show
Bug Best Practice introduced by
The property MaxValue does not exist on SilverStripe\UserForms\M...ld\EditableNumericField. Since you implemented __get, consider adding a @property annotation.
Loading history...
82
            $field->setAttribute('data-rule-max', $this->MaxValue);
83
        }
84
    }
85
86
    public function validate()
87
    {
88
        $result = parent::validate();
89
        if ($this->MinValue > $this->MaxValue) {
0 ignored issues
show
Bug Best Practice introduced by
The property MaxValue does not exist on SilverStripe\UserForms\M...ld\EditableNumericField. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property MinValue does not exist on SilverStripe\UserForms\M...ld\EditableNumericField. Since you implemented __get, consider adding a @property annotation.
Loading history...
90
            $result->addError(
91
                _t(__CLASS__ . '.ORDER_WARNING', 'Minimum length should be less than the maximum length.')
92
            );
93
        }
94
        return $result;
95
    }
96
}
97