Skip to content

Commit 4998162

Browse files
authored
Merge pull request #20 from dipcode-software/bugfix/form-data-files
Fixed visibility of data and files on form class
2 parents 66edb49 + e53d1ec commit 4998162

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [2.0.4] - 2017-12-14
8+
### Fixed
9+
- `data` and `files` were wrongly exposed on `Form` class. Changed visibility to private and added `getData` and `getFiles` methods.
10+
711
## [2.0.3] - 2017-12-13
812
### Added
913
- `setRequired` to `Field` class;

src/Bounds/BoundField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function getValue()
109109
{
110110
if ($this->form->isBound() && !$this->field->isDisabled()) {
111111
$widget = $this->field->getWidget();
112-
$value = $widget->valueFromData($this->form->data, $this->form->files, $this->html_name);
112+
$value = $widget->valueFromData($this->form->getData(), $this->form->getFiles(), $this->html_name);
113113
} else {
114114
$value = $this->form->getInitialForField($this->field, $this->name);
115115
}

src/Forms/Form.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ abstract class Form implements ArrayAccess, Iterator, Countable
1616
const PREFIX_FORMAT = '%s-%s';
1717
const NON_FIELD_ERRORS = '__all__';
1818

19+
/**
20+
* Array with form data.
21+
* @var array
22+
*/
23+
private $data = null;
24+
25+
/**
26+
* Array with form data files.
27+
* @var array
28+
*/
29+
private $files = null;
30+
31+
/**
32+
* Array with initial data.
33+
* @var array
34+
*/
35+
private $initial = array();
36+
1937
/**
2038
* List of form errors.
2139
* @var array
@@ -66,14 +84,15 @@ abstract class Form implements ArrayAccess, Iterator, Countable
6684

6785
/**
6886
* Constructor method
87+
*
6988
* @param array $args Arguments
7089
*/
7190
public function __construct(array $args = array())
7291
{
73-
$this->data = array_key_exists('data', $args) ? $args['data'] : null;
74-
$this->files = array_key_exists('files', $args) ? $args['files'] : null;
92+
$this->data = array_key_exists('data', $args) ? $args['data'] : $this->data;
93+
$this->files = array_key_exists('files', $args) ? $args['files'] : $this->files;
7594
$this->prefix = array_key_exists('prefix', $args) ? $args['prefix'] : $this->prefix;
76-
$this->initial = array_key_exists('initial', $args) ? $args['initial'] : array();
95+
$this->initial = array_key_exists('initial', $args) ? $args['initial'] : $this->initial;
7796
$this->css_classes = array_key_exists('css_classes', $args) ? $args['css_classes'] : $this->css_classes;
7897

7998
$this->is_bound = !is_null($this->data) or !is_null($this->files);
@@ -82,6 +101,7 @@ public function __construct(array $args = array())
82101

83102
/**
84103
* Method to be redefined with form fields
104+
*
85105
* @return array Desired fields for this form.
86106
*/
87107
protected static function setFields()
@@ -91,15 +111,37 @@ protected static function setFields()
91111

92112
/**
93113
* Return if form is bounded or not.
114+
*
94115
* @return boolean
95116
*/
96117
public function isBound()
97118
{
98119
return $this->is_bound;
99120
}
100121

122+
/**
123+
* Return form data array.
124+
*
125+
* @return array
126+
*/
127+
public function getData()
128+
{
129+
return $this->data;
130+
}
131+
132+
/**
133+
* Return form files data array.
134+
*
135+
* @return array
136+
*/
137+
public function getFiles()
138+
{
139+
return $this->files;
140+
}
141+
101142
/**
102143
* Return css classes to be added to each widget.
144+
*
103145
* @return array
104146
*/
105147
public function getCssClasses()
@@ -109,6 +151,7 @@ public function getCssClasses()
109151

110152
/**
111153
* Return error css class to be added on case of field error.
154+
*
112155
* @return array
113156
*/
114157
public function getErrorCssClass()
@@ -118,7 +161,9 @@ public function getErrorCssClass()
118161

119162
/**
120163
* Special method to make errors accessible as a attribute.
164+
*
121165
* @param string $name Attribute name.
166+
*
122167
* @return mixed
123168
*/
124169
public function __get(string $name)
@@ -134,7 +179,9 @@ public function __get(string $name)
134179

135180
/**
136181
* Add a prefix to a name.
182+
*
137183
* @param string $field_name Field name.
184+
*
138185
* @return string
139186
*/
140187
public function addPrefix(string $field_name)
@@ -148,6 +195,7 @@ public function addPrefix(string $field_name)
148195

149196
/**
150197
* Add error to specific $field_name, if null, define to NON_FIELD_ERRORS.
198+
*
151199
* @param mixed $error
152200
* @param string $field_name
153201
*/
@@ -222,6 +270,7 @@ private function cleanForm()
222270

223271
/**
224272
* Redefine if need to validate crossfields.
273+
*
225274
* @return array Cleaned data
226275
*/
227276
protected function clean()
@@ -231,6 +280,7 @@ protected function clean()
231280

232281
/**
233282
* Return cleaned data values.
283+
*
234284
* @return array Cleaned data
235285
*/
236286
public function getCleanedData()
@@ -240,7 +290,9 @@ public function getCleanedData()
240290

241291
/**
242292
* Return cleaned field value.
293+
*
243294
* @param string $field_name Name of field.
295+
*
244296
* @return string Cleaned field value.
245297
*/
246298
public function getCleanedField(string $field_name)
@@ -250,7 +302,9 @@ public function getCleanedField(string $field_name)
250302

251303
/**
252304
* Check if field has error on it.
305+
*
253306
* @param string $field_name Name of field to check
307+
*
254308
* @return boolean
255309
*/
256310
public function hasErrors($field_name)
@@ -260,7 +314,9 @@ public function hasErrors($field_name)
260314

261315
/**
262316
* Return all errors associated to $field_name.
317+
*
263318
* @param string $field_name Field name
319+
*
264320
* @return ErrorList
265321
*/
266322
public function getFieldErrors(string $field_name)
@@ -274,6 +330,7 @@ public function getFieldErrors(string $field_name)
274330

275331
/**
276332
* Return errors not associated with any field.
333+
*
277334
* @return ErrorList
278335
*/
279336
public function getNonFieldErrors()
@@ -287,6 +344,7 @@ public function getNonFieldErrors()
287344

288345
/**
289346
* Check if form is valid.
347+
*
290348
* @return bool
291349
*/
292350
public function isValid()
@@ -296,6 +354,7 @@ public function isValid()
296354

297355
/**
298356
* Check if form is valid.
357+
*
299358
* @return bool
300359
*/
301360
public function getInitialForField($field, $field_name)
@@ -327,6 +386,7 @@ public function offsetUnset($offset)
327386

328387
/**
329388
* @throws UnexpectedValueException
389+
*
330390
* @return BoundField
331391
*/
332392
public function offsetGet($offset)

tests/unit/Forms/FormTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ public function testConstructorWithBoundedForm()
2626
$this->assertAttributeEquals($data, "data", $form);
2727
}
2828

29+
public function testGetData()
30+
{
31+
$data = array("data" => "data");
32+
$form = new ExampleForm(["data" => $data]);
33+
34+
$this->assertEquals($form->getData(), $data);
35+
}
36+
37+
public function testGetFiles()
38+
{
39+
$files = array("files" => "files");
40+
$form = new ExampleForm(["files" => $files]);
41+
42+
$this->assertEquals($form->getFiles(), $files);
43+
}
44+
2945
public function testAddPrefix()
3046
{
3147
$form = new ExampleForm(["prefix" => "prefix"]);

0 commit comments

Comments
 (0)