Passed
Push — master ( e78c48...210134 )
by Damian
14:57 queued 06:02
created

TextField::getSchemaDataDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
    {
84
        $data = parent::getSchemaDataDefaults();
85
        $data['data']['maxlength'] =  $this->getMaxLength();
86
        return $data;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function InternallyLabelledField()
93
    {
94
        Deprecation::notice('4.0', 'Please use ->setValue() instead');
95
96
        if (!$this->value) {
97
            $this->value = $this->Title();
98
        }
99
100
        return $this->Field();
101
    }
102
103
    /**
104
     * Validate this field
105
     *
106
     * @param Validator $validator
107
     * @return bool
108
     */
109
    public function validate($validator)
110
    {
111
        if (!is_null($this->maxLength) && mb_strlen($this->value) > $this->maxLength) {
112
            $validator->validationError(
113
                $this->name,
114
                _t(
115
                    'SilverStripe\\Forms\\TextField.VALIDATEMAXLENGTH',
116
                    'The value for {name} must not exceed {maxLength} characters in length',
117
                    array('name' => $this->getName(), 'maxLength' => $this->maxLength)
118
                ),
119
                "validation"
120
            );
121
            return false;
122
        }
123
        return true;
124
    }
125
126
    public function getSchemaValidation()
127
    {
128
        $rules = parent::getSchemaValidation();
129
        if ($this->getMaxLength()) {
130
            $rules['max'] = [
131
                'length' => $this->getMaxLength(),
132
            ];
133
        }
134
        return $rules;
135
    }
136
}
137