Skip to content

Commit

Permalink
Add Field Formatter feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlpp committed Feb 7, 2016
1 parent f17ed09 commit 001c1b8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Dvlpp/Sharp/Config/SharpFormFieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ abstract class SharpFormFieldConfig
*/
protected $entity;

/**
* @var string
*/
protected $formatter;

/**
* @param string $label
* @return $this
Expand Down Expand Up @@ -160,4 +165,23 @@ public function entity()
{
return $this->entity;
}

/**
* @param string $formatter
* @return $this
*/
public function setFormatter($formatter)
{
$this->formatter = $formatter;

return $this;
}

/**
* @return string
*/
public function formatter()
{
return $this->formatter;
}
}
13 changes: 12 additions & 1 deletion src/Dvlpp/Sharp/Form/Fields/AbstractSharpField.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,18 @@ function __construct($field, $attributes, $instance, $listKey)

} else {
// Value is instance->key
$this->fieldValue = $instance ? $instance->{$field->key()} : null;
if(!$instance) {
$this->fieldValue = null;

} else {
$this->fieldValue = $instance->{$field->key()};

if(strlen($this->fieldValue) && $field->formatter()) {
// There's a field formatter (for value display in form)
$formatter = app($field->formatter());
$this->fieldValue = $formatter->fieldValue($this->fieldValue);
}
}
}
}

Expand Down

0 comments on commit 001c1b8

Please sign in to comment.