Skip to content

Commit

Permalink
Add form field valuator feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlpp committed Feb 11, 2016
1 parent c2a3764 commit ba71344
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/Dvlpp/Sharp/Config/SharpFormFieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ abstract class SharpFormFieldConfig
*/
protected $formatter;

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

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

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

return $this;
}

/**
* @return string
*/
public function valuator()
{
return $this->valuator;
}
}
11 changes: 8 additions & 3 deletions src/Dvlpp/Sharp/Form/Fields/AbstractSharpField.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,17 @@ function __construct($field, $attributes, $instance, $listKey)
$this->fieldValue = null;

} else {
$this->fieldValue = $instance->{$field->key()};
if($field->valuator()) {
// There's a field valuator, which override the model attribute
$this->fieldValue = app($field->valuator())->valuateField($instance, $field);

} 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);
$this->fieldValue = app($field->formatter())->formatFieldValue($this->fieldValue);
}
}
}
Expand Down

0 comments on commit ba71344

Please sign in to comment.