Completed
Push — master ( 40288a...c6c609 )
by Damian
34:49
created

UserForm::setupFormErrors()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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