Unify the logic for getting field values from a dataset
Amongst Form Widgets and "the" Form widget ping @alekseybobkov
This commit is contained in:
parent
54ecf8d33a
commit
b74b19741b
|
|
@ -395,4 +395,54 @@ class FormField
|
|||
|
||||
return Str::evalHtmlId($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this fields value from a supplied data set, which can be
|
||||
* an array or a model or another generic collection.
|
||||
* @param mixed $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValueFromData($data, $default = null)
|
||||
{
|
||||
$fieldName = $this->fieldName;
|
||||
|
||||
/*
|
||||
* Array field name, eg: field[key][key2][key3]
|
||||
*/
|
||||
$keyParts = Str::evalHtmlArray($fieldName);
|
||||
$lastField = end($keyParts);
|
||||
$result = $data;
|
||||
|
||||
/*
|
||||
* Loop the field key parts and build a value.
|
||||
* To support relations only the last field should return the
|
||||
* relation value, all others will look up the relation object as normal.
|
||||
*/
|
||||
foreach ($keyParts as $key) {
|
||||
|
||||
if ($result instanceof Model && $result->hasRelation($key)) {
|
||||
if ($key == $lastField) {
|
||||
$result = $result->getRelationValue($key) ?: $default;
|
||||
}
|
||||
else {
|
||||
$result = $result->{$key};
|
||||
}
|
||||
}
|
||||
elseif (is_array($result)) {
|
||||
if (!array_key_exists($key, $result)) {
|
||||
return $default;
|
||||
}
|
||||
$result = $result[$key];
|
||||
}
|
||||
else {
|
||||
if (!isset($result->{$key})) {
|
||||
return $default;
|
||||
}
|
||||
$result = $result->{$key};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,13 +103,7 @@ abstract class FormWidgetBase extends WidgetBase
|
|||
*/
|
||||
public function getLoadValue()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
|
||||
if (!is_null($model)) {
|
||||
return $model->{$attribute};
|
||||
}
|
||||
|
||||
return null;
|
||||
return $this->formField->getValueFromData($this->model);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -142,7 +142,6 @@ class RecordFinder extends FormWidgetBase
|
|||
*/
|
||||
public function prepareVars()
|
||||
{
|
||||
// This should be a relation and return a Model
|
||||
$this->relationModel = $this->getLoadValue();
|
||||
|
||||
$this->vars['value'] = $this->getKeyValue();
|
||||
|
|
@ -170,6 +169,20 @@ class RecordFinder extends FormWidgetBase
|
|||
return strlen($value) ? $value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLoadValue()
|
||||
{
|
||||
list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
||||
|
||||
if (!is_null($model)) {
|
||||
return $model->{$attribute};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getKeyValue()
|
||||
{
|
||||
if (!$this->relationModel) {
|
||||
|
|
|
|||
|
|
@ -709,49 +709,11 @@ class Form extends WidgetBase
|
|||
$field = $this->fields[$field];
|
||||
}
|
||||
|
||||
$fieldName = $field->fieldName;
|
||||
$defaultValue = (!$this->model->exists && $field->defaults !== '')
|
||||
? $field->defaults
|
||||
: null;
|
||||
|
||||
/*
|
||||
* Array field name, eg: field[key][key2][key3]
|
||||
*/
|
||||
$keyParts = Str::evalHtmlArray($fieldName);
|
||||
$lastField = end($keyParts);
|
||||
$result = $this->data;
|
||||
|
||||
/*
|
||||
* Loop the field key parts and build a value.
|
||||
* To support relations only the last field should return the
|
||||
* relation value, all others will look up the relation object as normal.
|
||||
*/
|
||||
foreach ($keyParts as $key) {
|
||||
|
||||
if ($result instanceof Model && $result->hasRelation($key)) {
|
||||
if ($key == $lastField) {
|
||||
$result = $result->getRelationValue($key) ?: $defaultValue;
|
||||
}
|
||||
else {
|
||||
$result = $result->{$key};
|
||||
}
|
||||
}
|
||||
elseif (is_array($result)) {
|
||||
if (!array_key_exists($key, $result)) {
|
||||
return $defaultValue;
|
||||
}
|
||||
$result = $result[$key];
|
||||
}
|
||||
else {
|
||||
if (!isset($result->{$key})) {
|
||||
return $defaultValue;
|
||||
}
|
||||
$result = $result->{$key};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
return $field->getValueFromData($this->data, $defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue