Skip to content

Commit

Permalink
Accept path for NamedFormItems
Browse files Browse the repository at this point in the history
This allows multiple forms in the same page by allowing prefixes in form elements items.
You could use "form1.input1" for an input name for instance and only input1 will be used to
read and write the values to the model instance.
  • Loading branch information
alexrosenfeld committed Jul 10, 2015
1 parent ce4ed6a commit 8f0fd9f
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/SleepingOwl/Admin/FormItems/NamedFormItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,46 @@
abstract class NamedFormItem extends BaseFormItem
{

protected $path;
protected $name;
protected $attribute;
protected $label;
protected $defaultValue;
protected $readonly;

function __construct($name, $label = null)
function __construct($path, $label = null)
{
$this->label = $label;
$this->name = $name;
$parts = explode(".", $path);
if (count($parts) > 1) {
$this->path = $path;
$this->name = $parts[0] . "[" . implode("][", array_slice($parts, 1)) . "]";
$this->attribute = implode(".", array_slice(explode(".", $path), -1, 1));
} else {
$this->path = $path;
$this->name = $path;
$this->attribute = $path;
}
}

public function path($path = null)
{
if (is_null($path))
{
return $this->path;
}
$this->path = $path;
return $path;
}

public function attribute($attribute = null)
{
if (is_null($attribute))
{
return $this->attribute;
}
$this->attribute = $attribute;
return $attribute;
}

public function name($name = null)
Expand All @@ -40,6 +71,7 @@ public function getParams()
{
return parent::getParams() + [
'name' => $this->name(),
'attribute' => $this->attribute(),
'label' => $this->label(),
'readonly' => $this->readonly(),
'value' => $this->value()
Expand Down Expand Up @@ -71,16 +103,16 @@ public function readonly($readonly = null)
public function value()
{
$instance = $this->instance();
if ( ! is_null($value = old($this->name())))
if ( ! is_null($value = old($this->path())))
{
return $value;
}
$input = Input::all();
if (array_key_exists($this->name, $input))
if (($value = array_get($input, $this->path())) !== null)
{
return Input::get($this->name());
return $value; //Input::get($this->name());
}
if ( ! is_null($instance) && ! is_null($value = $instance->getAttribute($this->name())))
if ( ! is_null($instance) && ! is_null($value = $instance->getAttribute($this->attribute())))
{
return $value;
}
Expand All @@ -89,12 +121,15 @@ public function value()

public function save()
{
$name = $this->name();
if ( ! Input::has($name))
//$name = $this->name();
$attribute = $this->attribute();
if (Input::get($this->path()) == null)
{
Input::merge([$name => null]);
$value = null; //Input::merge(([$name => null]);
} else {
$value = $this->value();
}
$this->instance()->$name = $this->value();
$this->instance()->$attribute = $value;
}

public function required()
Expand Down

0 comments on commit 8f0fd9f

Please sign in to comment.