Completed
Push — master ( b537e6...b70c66 )
by Daniel
35:35
created

UserForm::setupFormErrors()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 1
f 0
cc 3
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * @package userforms
5
 */
6
class UserForm extends Form {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
8
	/**
9
	 * @param Controller $controller
10
	 * @param string $name
11
	 */
12
	public function __construct(Controller $controller, $name = 'Form') {
13
14
		$this->controller = $controller;
15
		$this->setRedirectToFormOnValidationError(true);
16
17
		parent::__construct(
18
			$controller,
19
			$name,
20
			new FieldList(),
21
			new FieldList()
22
		);
23
24
		$this->setFields($fields = $this->getFormFields());
25
		$fields->setForm($this);
26
		$this->setActions($actions = $this->getFormActions());
27
		$actions->setForm($this);
28
		$this->setValidator($this->getRequiredFields());
29
30
		// This needs to be re-evaluated since fields have been assigned
31
		$this->setupFormErrors();
32
33
		// Number each page
34
		$stepNumber = 1;
35
		foreach($this->getSteps() as $step) {
36
			$step->setStepNumber($stepNumber++);
37
		}
38
39
		if($controller->DisableCsrfSecurityToken) {
40
			$this->disableSecurityToken();
41
		}
42
43
		$data = Session::get("FormInfo.{$this->FormName()}.data");
44
45
		if(is_array($data)) {
46
			$this->loadDataFrom($data);
47
		}
48
49
		$this->extend('updateForm');
50
	}
51
52
	public function setupFormErrors()
53
	{
54
		// Suppress setupFormErrors if fields haven't been bootstrapped
55
		if($this->fields && $this->fields->exists()) {
56
			return parent::setupFormErrors();
57
		}
58
59
		return $this;
60
	}
61
62
	/**
63
	 * Used for partial caching in the template.
64
	 *
65
	 * @return string
66
	 */
67
	public function getLastEdited() {
68
		return $this->controller->LastEdited;
69
	}
70
71
	/**
72
	 * @return bool
73
	 */
74
	public function getDisplayErrorMessagesAtTop() {
75
		return (bool)$this->controller->DisplayErrorMessagesAtTop;
76
	}
77
78
	/**
79
	 * Return the fieldlist, filtered to only contain steps
80
	 *
81
	 * @return ArrayList
82
	 */
83
	public function getSteps() {
84
		return $this->Fields()->filterByCallback(function($field) {
85
			return $field instanceof UserFormsStepField;
86
		});
87
	}
88
89
	/**
90
	 * Get the form fields for the form on this page. Can modify this FieldSet
91
	 * by using {@link updateFormFields()} on an {@link Extension} subclass which
92
	 * is applied to this controller.
93
	 *
94
	 * This will be a list of top level composite steps
95
	 *
96
	 * @return FieldList
97
	 */
98
	public function getFormFields() {
99
		$fields = new UserFormsFieldList();
100
		$target = $fields;
101
		foreach ($this->controller->Fields() as $field) {
102
			$target = $target->processNext($field);
103
		}
104
		$fields->clearEmptySteps();
105
		$this->extend('updateFormFields', $fields);
106
		$fields->setForm($this);
107
		return $fields;
108
	}
109
110
	/**
111
	 * Generate the form actions for the UserDefinedForm. You
112
	 * can manipulate these by using {@link updateFormActions()} on
113
	 * a decorator.
114
	 *
115
	 * @todo Make form actions editable via their own field editor.
116
	 *
117
	 * @return FieldList
118
	 */
119
	public function getFormActions() {
120
		$submitText = ($this->controller->SubmitButtonText) ? $this->controller->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit');
121
		$clearText = ($this->controller->ClearButtonText) ? $this->controller->ClearButtonText : _t('UserDefinedForm.CLEARBUTTON', 'Clear');
122
123
		$actions = new FieldList(
124
			new FormAction("process", $submitText)
125
		);
126
127
		if($this->controller->ShowClearButton) {
128
			$actions->push(new ResetFormAction("clearForm", $clearText));
129
		}
130
131
		$this->extend('updateFormActions', $actions);
132
		$actions->setForm($this);
133
		return $actions;
134
	}
135
136
	/**
137
	 * Get the required form fields for this form.
138
	 *
139
	 * @return RequiredFields
140
	 */
141
	public function getRequiredFields() {
142
		// Generate required field validator
143
		$requiredNames = $this
144
			->getController()
145
			->Fields()
146
			->filter('Required', true)
147
			->column('Name');
148
		$required = new RequiredFields($requiredNames);
149
		$this->extend('updateRequiredFields', $required);
150
		$required->setForm($this);
151
		return $required;
152
	}
153
154
	/**
155
	 * Override some we can add UserForm specific attributes to the form.
156
	 *
157
	 * @return array
158
	 */
159
	public function getAttributes() {
160
		$attrs = parent::getAttributes();
161
162
		$attrs['class'] = $attrs['class'] . ' userform';
163
		$attrs['data-livevalidation'] = (bool)$this->controller->EnableLiveValidation;
164
		$attrs['data-toperrors'] = (bool)$this->controller->DisplayErrorMessagesAtTop;
165
		$attrs['data-hidefieldlabels'] = (bool)$this->controller->HideFieldLabels;
166
167
		return $attrs;
168
	}
169
}
170