Completed
Push — master ( 85408a...305cf3 )
by
unknown
15s
created

EditableTextField::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Control\Email\Email;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\FieldGroup;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\NumericField;
10
use SilverStripe\Forms\TextareaField;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\UserForms\Model\EditableFormField;
13
14
/**
15
 * EditableTextField
16
 *
17
 * This control represents a user-defined text field in a user defined form
18
 *
19
 * @package userforms
20
 */
21
22
class EditableTextField extends EditableFormField
23
{
24
    private static $singular_name = 'Text Field';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
25
26
    private static $plural_name = 'Text Fields';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
27
28
    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...
29
30
    /** @skipUpgrade */
31
    private static $autocomplete_options = [
0 ignored issues
show
introduced by
The private property $autocomplete_options is not used, and could be removed.
Loading history...
32
        'off' => 'Off',
33
        'on' => 'On',
34
        'name' => 'Full name',
35
        'honorific-prefix' => 'Prefix or title',
36
        'given-name' => 'First name',
37
        'additional-name' => 'Additional name',
38
        'family-name' => 'Family name',
39
        'honorific-suffix' => 'Suffix (e.g Jr.)',
40
        'nickname' => 'Nickname',
41
        'email' => 'Email',
42
        'organization-title' => 'Job title',
43
        'organization' => 'Organization',
44
        'street-address' => 'Street address',
45
        'address-line1' => 'Address line 1',
46
        'address-line2' => 'Address line 2',
47
        'address-line3' => 'Address line 3',
48
        'address-level1' => 'Address level 1',
49
        'address-level2' => 'Address level 2',
50
        'address-level3' => 'Address level 3',
51
        'address-level4' => 'Address level 4',
52
        'country' => 'Country',
53
        'country-name' => 'Country name',
54
        'postal-code' => 'Postal code',
55
        'bday' => 'Birthday',
56
        'sex' => 'Gender identity',
57
        'tel' => 'Telephone number',
58
        'url' => 'Home page'
59
    ];
60
61
    protected $jsEventHandler = 'keyup';
62
63
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
64
        'MinLength' => 'Int',
65
        'MaxLength' => 'Int',
66
        'Rows' => 'Int(1)',
67
        'Autocomplete' => 'Varchar(255)'
68
    ];
69
70
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
71
        'Rows' => 1
72
    ];
73
74
    private static $table_name = 'EditableTextField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
75
76
    public function getCMSFields()
77
    {
78
        $this->beforeUpdateCMSFields(function ($fields) {
79
            $fields->addFieldsToTab(
80
                'Root.Main',
81
                [
82
                    NumericField::create(
83
                        'Rows',
84
                        _t(__CLASS__.'.NUMBERROWS', 'Number of rows')
85
                    )->setDescription(_t(
86
                        __CLASS__.'.NUMBERROWS_DESCRIPTION',
87
                        'Fields with more than one row will be generated as a textarea'
88
                    )),
89
                    DropdownField::create(
90
                        'Autocomplete',
91
                        _t(__CLASS__.'.AUTOCOMPLETE', 'Autocomplete'),
92
                        $this->config()->get('autocomplete_options')
93
                    )->setDescription(_t(
94
                        __CLASS__.'.AUTOCOMPLETE_DESCRIPTION',
95
                        'Supported browsers will attempt to populate this field automatically with the users information, use to set the value populated'
96
                    ))
97
                ]
98
            );
99
        });
100
101
        return parent::getCMSFields();
102
    }
103
104
    /**
105
     * @return ValidationResult
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...mField\ValidationResult 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...
106
     */
107
    public function validate()
108
    {
109
        $result = parent::validate();
110
111
        if ($this->MinLength > $this->MaxLength) {
0 ignored issues
show
Bug Best Practice introduced by
The property MinLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property MaxLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
112
            $result->addError(_t(
113
                __CLASS__ . 'MINMAXLENGTHCHECK',
114
                'Minimum length should be less than the Maximum length.'
115
            ));
116
        }
117
118
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type SilverStripe\ORM\ValidationResult which is incompatible with the documented return type SilverStripe\UserForms\M...mField\ValidationResult.
Loading history...
119
    }
120
121
    /**
122
     * @return FieldList
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ableFormField\FieldList 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...
123
     */
124
    public function getFieldValidationOptions()
125
    {
126
        $fields = parent::getFieldValidationOptions();
127
128
        $fields->merge([
129
            FieldGroup::create(
130
                _t(__CLASS__.'.TEXTLENGTH', 'Allowed text length'),
131
                [
132
                    NumericField::create('MinLength', false),
133
                    LiteralField::create('RangeLength', _t(__CLASS__.".RANGE_TO", "to")),
134
                    NumericField::create('MaxLength', false)
135
136
                ]
137
            )
138
        ]);
139
140
        return $fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fields returns the type SilverStripe\Forms\FieldList which is incompatible with the documented return type SilverStripe\UserForms\M...ableFormField\FieldList.
Loading history...
141
    }
142
143
    /**
144
     * @return TextareaField|TextField
145
     */
146
    public function getFormField()
147
    {
148
        if ($this->Rows > 1) {
0 ignored issues
show
Bug Best Practice introduced by
The property Rows does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
149
            $field = TextareaField::create($this->Name, $this->Title ?: false, $this->Default)
150
                ->setFieldHolderTemplate(EditableFormField::class . '_holder')
151
                ->setTemplate(str_replace('EditableTextField', 'EditableTextareaField', __CLASS__))
152
                ->setRows($this->Rows);
153
        } else {
154
            $field = TextField::create($this->Name, $this->Title ?: false, $this->Default)
155
                ->setFieldHolderTemplate(EditableFormField::class . '_holder')
156
                ->setTemplate(EditableFormField::class);
157
        }
158
159
        $this->doUpdateFormField($field);
160
161
        return $field;
162
    }
163
164
    /**
165
     * Updates a formfield with the additional metadata specified by this field
166
     *
167
     * @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...
168
     */
169
    protected function updateFormField($field)
170
    {
171
        parent::updateFormField($field);
172
173
        if (is_numeric($this->MinLength) && $this->MinLength > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The property MinLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
174
            $field->setAttribute('data-rule-minlength', intval($this->MinLength));
175
        }
176
177
        if (is_numeric($this->MaxLength) && $this->MaxLength > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The property MaxLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
178
            if ($field instanceof TextField) {
179
                $field->setMaxLength(intval($this->MaxLength));
180
            }
181
            $field->setAttribute('data-rule-maxlength', intval($this->MaxLength));
182
        }
183
184
        if ($this->Autocomplete) {
0 ignored issues
show
Bug Best Practice introduced by
The property Autocomplete does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
185
            $field->setAttribute('autocomplete', $this->Autocomplete);
186
        }
187
    }
188
}
189