Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Bounds/BoundField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
66 changes: 63 additions & 3 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -91,15 +111,37 @@ protected static function setFields()

/**
* Return if form is bounded or not.
*
* @return boolean
*/
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()
Expand All @@ -109,6 +151,7 @@ public function getCssClasses()

/**
* Return error css class to be added on case of field error.
*
* @return array
*/
public function getErrorCssClass()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
*/
Expand Down Expand Up @@ -222,6 +270,7 @@ private function cleanForm()

/**
* Redefine if need to validate crossfields.
*
* @return array Cleaned data
*/
protected function clean()
Expand All @@ -231,6 +280,7 @@ protected function clean()

/**
* Return cleaned data values.
*
* @return array Cleaned data
*/
public function getCleanedData()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -274,6 +330,7 @@ public function getFieldErrors(string $field_name)

/**
* Return errors not associated with any field.
*
* @return ErrorList
*/
public function getNonFieldErrors()
Expand All @@ -287,6 +344,7 @@ public function getNonFieldErrors()

/**
* Check if form is valid.
*
* @return bool
*/
public function isValid()
Expand All @@ -296,6 +354,7 @@ public function isValid()

/**
* Check if form is valid.
*
* @return bool
*/
public function getInitialForField($field, $field_name)
Expand Down Expand Up @@ -327,6 +386,7 @@ public function offsetUnset($offset)

/**
* @throws UnexpectedValueException
*
* @return BoundField
*/
public function offsetGet($offset)
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down