diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e31409..b3356eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [2.0.4] - 2017-12-14 +### Fixed + - `data` and `files` were wrongly exposed on `Form` class. Changed visibility to private and added `getData` and `getFiles` methods. + ## [2.0.3] - 2017-12-13 ### Added - `setRequired` to `Field` class; diff --git a/src/Bounds/BoundField.php b/src/Bounds/BoundField.php index 59090c5..803c8f1 100644 --- a/src/Bounds/BoundField.php +++ b/src/Bounds/BoundField.php @@ -109,7 +109,7 @@ protected function getValue() { if ($this->form->isBound() && !$this->field->isDisabled()) { $widget = $this->field->getWidget(); - $value = $widget->valueFromData($this->form->data, $this->form->files, $this->html_name); + $value = $widget->valueFromData($this->form->getData(), $this->form->getFiles(), $this->html_name); } else { $value = $this->form->getInitialForField($this->field, $this->name); } diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 2702832..b86b924 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -16,6 +16,24 @@ abstract class Form implements ArrayAccess, Iterator, Countable const PREFIX_FORMAT = '%s-%s'; const NON_FIELD_ERRORS = '__all__'; + /** + * Array with form data. + * @var array + */ + private $data = null; + + /** + * Array with form data files. + * @var array + */ + private $files = null; + + /** + * Array with initial data. + * @var array + */ + private $initial = array(); + /** * List of form errors. * @var array @@ -66,14 +84,15 @@ abstract class Form implements ArrayAccess, Iterator, Countable /** * Constructor method + * * @param array $args Arguments */ public function __construct(array $args = array()) { - $this->data = array_key_exists('data', $args) ? $args['data'] : null; - $this->files = array_key_exists('files', $args) ? $args['files'] : null; + $this->data = array_key_exists('data', $args) ? $args['data'] : $this->data; + $this->files = array_key_exists('files', $args) ? $args['files'] : $this->files; $this->prefix = array_key_exists('prefix', $args) ? $args['prefix'] : $this->prefix; - $this->initial = array_key_exists('initial', $args) ? $args['initial'] : array(); + $this->initial = array_key_exists('initial', $args) ? $args['initial'] : $this->initial; $this->css_classes = array_key_exists('css_classes', $args) ? $args['css_classes'] : $this->css_classes; $this->is_bound = !is_null($this->data) or !is_null($this->files); @@ -82,6 +101,7 @@ public function __construct(array $args = array()) /** * Method to be redefined with form fields + * * @return array Desired fields for this form. */ protected static function setFields() @@ -91,6 +111,7 @@ protected static function setFields() /** * Return if form is bounded or not. + * * @return boolean */ public function isBound() @@ -98,8 +119,29 @@ public function isBound() return $this->is_bound; } + /** + * Return form data array. + * + * @return array + */ + public function getData() + { + return $this->data; + } + + /** + * Return form files data array. + * + * @return array + */ + public function getFiles() + { + return $this->files; + } + /** * Return css classes to be added to each widget. + * * @return array */ public function getCssClasses() @@ -109,6 +151,7 @@ public function getCssClasses() /** * Return error css class to be added on case of field error. + * * @return array */ public function getErrorCssClass() @@ -118,7 +161,9 @@ public function getErrorCssClass() /** * Special method to make errors accessible as a attribute. + * * @param string $name Attribute name. + * * @return mixed */ public function __get(string $name) @@ -134,7 +179,9 @@ public function __get(string $name) /** * Add a prefix to a name. + * * @param string $field_name Field name. + * * @return string */ public function addPrefix(string $field_name) @@ -148,6 +195,7 @@ public function addPrefix(string $field_name) /** * Add error to specific $field_name, if null, define to NON_FIELD_ERRORS. + * * @param mixed $error * @param string $field_name */ @@ -222,6 +270,7 @@ private function cleanForm() /** * Redefine if need to validate crossfields. + * * @return array Cleaned data */ protected function clean() @@ -231,6 +280,7 @@ protected function clean() /** * Return cleaned data values. + * * @return array Cleaned data */ public function getCleanedData() @@ -240,7 +290,9 @@ public function getCleanedData() /** * Return cleaned field value. + * * @param string $field_name Name of field. + * * @return string Cleaned field value. */ public function getCleanedField(string $field_name) @@ -250,7 +302,9 @@ public function getCleanedField(string $field_name) /** * Check if field has error on it. + * * @param string $field_name Name of field to check + * * @return boolean */ public function hasErrors($field_name) @@ -260,7 +314,9 @@ public function hasErrors($field_name) /** * Return all errors associated to $field_name. + * * @param string $field_name Field name + * * @return ErrorList */ public function getFieldErrors(string $field_name) @@ -274,6 +330,7 @@ public function getFieldErrors(string $field_name) /** * Return errors not associated with any field. + * * @return ErrorList */ public function getNonFieldErrors() @@ -287,6 +344,7 @@ public function getNonFieldErrors() /** * Check if form is valid. + * * @return bool */ public function isValid() @@ -296,6 +354,7 @@ public function isValid() /** * Check if form is valid. + * * @return bool */ public function getInitialForField($field, $field_name) @@ -327,6 +386,7 @@ public function offsetUnset($offset) /** * @throws UnexpectedValueException + * * @return BoundField */ public function offsetGet($offset) diff --git a/tests/unit/Forms/FormTest.php b/tests/unit/Forms/FormTest.php index 46c2658..c36d78d 100644 --- a/tests/unit/Forms/FormTest.php +++ b/tests/unit/Forms/FormTest.php @@ -26,6 +26,22 @@ public function testConstructorWithBoundedForm() $this->assertAttributeEquals($data, "data", $form); } + public function testGetData() + { + $data = array("data" => "data"); + $form = new ExampleForm(["data" => $data]); + + $this->assertEquals($form->getData(), $data); + } + + public function testGetFiles() + { + $files = array("files" => "files"); + $form = new ExampleForm(["files" => $files]); + + $this->assertEquals($form->getFiles(), $files); + } + public function testAddPrefix() { $form = new ExampleForm(["prefix" => "prefix"]);