Passed
Pull Request — 4.0 (#7676)
by
unknown
07:28
created

TextField   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A getMaxLength() 0 3 1
A setMaxLength() 0 5 1
A getAttributes() 0 14 2
A getSchemaValidation() 0 9 2
A getSchemaDataDefaults() 0 9 2
A InternallyLabelledField() 0 9 2
A validate() 0 15 3
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\Dev\Deprecation;
6
7
/**
8
 * Text input field.
9
 */
10
class TextField extends FormField
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $maxLength;
16
17
    protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
18
19
    /**
20
     * Returns an input field.
21
     *
22
     * @param string $name
23
     * @param null|string $title
24
     * @param string $value
25
     * @param null|int $maxLength Max characters to allow for this field. If this value is stored
26
     * against a DB field with a fixed size it's recommended to set an appropriate max length
27
     * matching this size.
28
     * @param null|Form $form
29
     */
30
    public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null)
31
    {
32
        if ($maxLength) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxLength of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
33
            $this->setMaxLength($maxLength);
34
        }
35
36
        if ($form) {
37
            $this->setForm($form);
38
        }
39
40
        parent::__construct($name, $title, $value);
41
    }
42
43
    /**
44
     * @param int $maxLength
45
     * @return $this
46
     */
47
    public function setMaxLength($maxLength)
48
    {
49
        $this->maxLength = $maxLength;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return null|int
56
     */
57
    public function getMaxLength()
58
    {
59
        return $this->maxLength;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getAttributes()
66
    {
67
        $maxLength = $this->getMaxLength();
68
69
        $attributes = array();
70
71
        if ($maxLength) {
72
            $attributes['maxLength'] = $maxLength;
73
            $attributes['size'] = min($maxLength, 30);
74
        }
75
76
        return array_merge(
77
            parent::getAttributes(),
78
            $attributes
79
        );
80
    }
81
82
    public function getSchemaDataDefaults() {
83
        $data = parent::getSchemaDataDefaults();
84
85
        $maxLength = $this->getMaxLength();
86
        if ($maxLength) {
87
            $data['maxLength'] = $maxLength;
88
        }
89
90
        return $data;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function InternallyLabelledField()
97
    {
98
        Deprecation::notice('4.0', 'Please use ->setValue() instead');
99
100
        if (!$this->value) {
101
            $this->value = $this->Title();
102
        }
103
104
        return $this->Field();
105
    }
106
107
    /**
108
     * Validate this field
109
     *
110
     * @param Validator $validator
111
     * @return bool
112
     */
113
    public function validate($validator)
114
    {
115
        if (!is_null($this->maxLength) && mb_strlen($this->value) > $this->maxLength) {
116
            $validator->validationError(
117
                $this->name,
118
                _t(
119
                    'SilverStripe\\Forms\\TextField.VALIDATEMAXLENGTH',
120
                    'The value for {name} must not exceed {maxLength} characters in length',
121
                    array('name' => $this->getName(), 'maxLength' => $this->maxLength)
122
                ),
123
                "validation"
124
            );
125
            return false;
126
        }
127
        return true;
128
    }
129
130
    public function getSchemaValidation()
131
    {
132
        $rules = parent::getSchemaValidation();
133
        if ($this->getMaxLength()) {
134
            $rules['max'] = [
135
                'length' => $this->getMaxLength(),
136
            ];
137
        }
138
        return $rules;
139
    }
140
}
141