Completed
Push — master ( 4ee78f...ce1053 )
by Damian
14:52 queued 39s
created

TextField::getSchemaValidation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
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
	 * @var int
13
	 */
14
	protected $maxLength;
15
16
	protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
17
18
	/**
19
	 * Returns an input field.
20
	 *
21
	 * @param string $name
22
	 * @param null|string $title
23
	 * @param string $value
24
	 * @param null|int $maxLength
25
	 * @param null|Form $form
26
	 */
27
	public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null) {
28
		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 zero. 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...
29
			$this->setMaxLength($maxLength);
30
		}
31
32
		if($form) {
33
			$this->setForm($form);
34
		}
35
36
		parent::__construct($name, $title, $value);
37
	}
38
39
	/**
40
	 * @param int $maxLength
41
	 *
42
	 * @return static
43
	 */
44
	public function setMaxLength($maxLength) {
45
		$this->maxLength = $maxLength;
46
47
		return $this;
48
	}
49
50
	/**
51
	 * @return null|int
52
	 */
53
	public function getMaxLength() {
54
		return $this->maxLength;
55
	}
56
57
	/**
58
	 * @return array
59
	 */
60
	public function getAttributes() {
61
		$maxLength = $this->getMaxLength();
62
63
		$attributes = array();
64
65
		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 zero. 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...
66
			$attributes['maxLength'] = $maxLength;
67
			$attributes['size'] = min($maxLength, 30);
68
		}
69
70
		return array_merge(
71
			parent::getAttributes(),
72
			$attributes
73
		);
74
	}
75
76
	/**
77
	 * @return string
78
	 */
79
	public function InternallyLabelledField() {
80
		Deprecation::notice('4.0', 'Please use ->setValue() instead');
81
82
		if(!$this->value) {
83
			$this->value = $this->Title();
84
		}
85
86
		return $this->Field();
87
	}
88
89
	/**
90
	 * Validate this field
91
	 *
92
	 * @param Validator $validator
93
	 * @return bool
94
	 */
95
	public function validate($validator) {
96
		if(!is_null($this->maxLength) && mb_strlen($this->value) > $this->maxLength) {
97
			$validator->validationError(
98
				$this->name,
99
				_t(
100
					'TextField.VALIDATEMAXLENGTH',
101
					'The value for {name} must not exceed {maxLength} characters in length',
102
					array('name' => $this->getName(), 'maxLength' => $this->maxLength)
103
				),
104
				"validation"
105
			);
106
			return false;
107
		}
108
		return true;
109
	}
110
111
	public function getSchemaValidation() {
112
		$rules = parent::getSchemaValidation();
113
		if ($this->getMaxLength()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getMaxLength() of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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...
114
			$rules['max'] = $this->getMaxLength();
115
		}
116
		return $rules;
117
	}
118
119
}
120