From 001c1b8b55350578048652ccea3811328363a585 Mon Sep 17 00:00:00 2001 From: Philippe Lonchampt Date: Sun, 7 Feb 2016 15:14:55 +0100 Subject: [PATCH] Add Field Formatter feature --- .../Sharp/Config/SharpFormFieldConfig.php | 24 +++++++++++++++++++ .../Sharp/Form/Fields/AbstractSharpField.php | 13 +++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Dvlpp/Sharp/Config/SharpFormFieldConfig.php b/src/Dvlpp/Sharp/Config/SharpFormFieldConfig.php index e076c4b..2e98e8a 100644 --- a/src/Dvlpp/Sharp/Config/SharpFormFieldConfig.php +++ b/src/Dvlpp/Sharp/Config/SharpFormFieldConfig.php @@ -45,6 +45,11 @@ abstract class SharpFormFieldConfig */ protected $entity; + /** + * @var string + */ + protected $formatter; + /** * @param string $label * @return $this @@ -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; + } } \ No newline at end of file diff --git a/src/Dvlpp/Sharp/Form/Fields/AbstractSharpField.php b/src/Dvlpp/Sharp/Form/Fields/AbstractSharpField.php index 326c9b2..ac5046a 100644 --- a/src/Dvlpp/Sharp/Form/Fields/AbstractSharpField.php +++ b/src/Dvlpp/Sharp/Form/Fields/AbstractSharpField.php @@ -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); + } + } } }