Completed
Push — 3.7 ( 81b2d8...ef0909 )
by
unknown
09:42
created

Validator::requireField()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 2
dl 0
loc 19
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This validation class handles all form and custom form validation through the use of Required
5
 * fields. It relies on javascript for client-side validation, and marking fields after server-side
6
 * validation. It acts as a visitor to individual form fields.
7
 *
8
 * @package forms
9
 * @subpackage validators
10
 */
11
abstract class Validator extends SS_Object {
12
13
	/**
14
	 * @var Form $form
15
	 */
16
	protected $form;
17
18
	/**
19
	 * @var array $errors
20
	 */
21
	protected $errors;
22
23
	/**
24
	 * @param Form $form
25
	 *
26
	 * @return $this
27
	 */
28
	public function setForm($form) {
29
		$this->form = $form;
30
31
		return $this;
32
	}
33
34
	/**
35
	 * Returns any errors there may be.
36
	 *
37
	 * @return null|array
38
	 */
39
	public function validate() {
40
		$this->errors = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $errors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
42
		$this->php($this->form->getData());
43
44
		return $this->errors;
45
	}
46
47
	/**
48
	 * Callback to register an error on a field (Called from implementations of
49
	 * {@link FormField::validate}). The optional error message type parameter is loaded into the
50
	 * HTML class attribute.
51
	 *
52
	 * See {@link getErrors()} for details.
53
	 *
54
	 * @param string $fieldName
55
	 * @param string $errorMessage
56
	 * @param string $errorMessageType
57
	 */
58
	public function validationError($fieldName, $errorMessage, $errorMessageType = '') {
59
		$this->errors[] = array(
60
			'fieldName' => $fieldName,
61
			'message' => $errorMessage,
62
			'messageType' => $errorMessageType,
63
		);
64
	}
65
66
	/**
67
	 * Returns all errors found by a previous call to {@link validate()}. The returned array has a
68
	 * structure resembling:
69
	 *
70
	 * <code>
71
	 *     array(
72
	 *         'fieldName' => '[form field name]',
73
	 *         'message' => '[validation error message]',
74
	 *         'messageType' => '[bad|message|validation|required]',
75
	 *     )
76
	 * </code>
77
	 *
78
	 * @return null|array
79
	 */
80
	public function getErrors() {
81
		return $this->errors;
82
	}
83
84
	/**
85
	 * @param string $fieldName
86
	 * @param array $data
87
	 */
88
	public function requireField($fieldName, $data) {
89
		if(is_array($data[$fieldName]) && count($data[$fieldName])) {
90
			foreach($data[$fieldName] as $componentKey => $componentValue) {
91
				if(!strlen($componentValue)) {
92
					$this->validationError(
93
						$fieldName,
94
						sprintf('%s %s is required', $fieldName, $componentKey),
95
						'required'
96
					);
97
				}
98
			}
99
		} else if(!strlen($data[$fieldName])) {
100
			$this->validationError(
101
				$fieldName,
102
				sprintf('%s is required', $fieldName),
103
				'required'
104
			);
105
		}
106
	}
107
108
	/**
109
	 * Returns whether the field in question is required. This will usually display '*' next to the
110
	 * field. The base implementation always returns false.
111
	 *
112
	 * @param string $fieldName
113
	 *
114
	 * @return bool
115
	 */
116
	public function fieldIsRequired($fieldName) {
117
		return false;
118
	}
119
120
	/**
121
	 * @param array $data
122
	 *
123
	 * @return mixed
124
	 */
125
	abstract public function php($data);
126
}
127