Skip to content

Commit

Permalink
Better handling of toArray for form data
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jan 20, 2018
1 parent fa1f82e commit 3204efe
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/
namespace UserFrosting\Sprinkle\FormGenerator;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface;
use UserFrosting\Sprinkle\Core\Facades\Debug;

/**
* Form Class
Expand Down Expand Up @@ -38,7 +39,6 @@ class Form {
/**
* Constructor
*
* @access public
* @param RequestSchemaInterface $schema
* @param array|object $data (default: [])
* @return void
Expand All @@ -56,10 +56,12 @@ public function __construct(RequestSchemaInterface $schema, $data = [])
*/
public function setData($data)
{
if (is_array($data)) {
if ($data instanceof Collection || $data instanceof Model) {
$this->data = $data->toArray();
} else if (is_array($data)) {
$this->data = $data;
} else {
$this->data = $data->toArray();
throw new \InvalidArgumentException("Data must be an array or a Collection");
}
}

Expand All @@ -76,7 +78,6 @@ public function setSchema(RequestSchemaInterface $schema)
/**
* Use to define the value of a form input when `setData` is already set
*
* @access public
* @param mixed $inputName
* @param mixed $value
* @return void
Expand All @@ -91,7 +92,6 @@ public function setValue($inputName, $value)
* Can also be used to overwrite an argument hardcoded in the Twig file.
* Use `setCustomFormData` to set any other tag.
*
* @access public
* @param string $inputName The input name where the argument will be added
* @param string $property The argument name. Example "data-color"
* @param string $data The value of the argument
Expand All @@ -113,7 +113,6 @@ public function setInputArgument($inputName, $property, $data)
* Function used to set options of a select element. Shortcut for using
* `setInputArgument` and `setValue`.
*
* @access public
* @param string $inputName The select name to add options to
* @param array $data An array of `value => label` options
* @param string $selected The selected key
Expand All @@ -136,7 +135,6 @@ public function setOptions($inputName, $data = [], $selected = null)
* Useful when using multiple schemas at once or if the names are using dot syntaxt.
* See : http://stackoverflow.com/a/20365198/445757
*
* @access public
* @param string $namespace
* @return void
*/
Expand All @@ -149,7 +147,6 @@ public function setFormNamespace($namespace)
* Generate an array contining all nececerry value to generate a form
* with Twig.
*
* @access public
* @return array The form fields data
*/
public function generate()
Expand Down

0 comments on commit 3204efe

Please sign in to comment.