DRY up code from #1504
This commit is contained in:
parent
7d911c2c9f
commit
ef22ac934d
|
|
@ -543,12 +543,65 @@ class FormField
|
|||
* Returns this fields value from a supplied data set, which can be
|
||||
* an array or a model or another generic collection.
|
||||
* @param mixed $data
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValueFromData($data, $default = null)
|
||||
{
|
||||
$fieldName = $this->valueFrom ?: $this->fieldName;
|
||||
return $this->getFieldNameFromData($fieldName, $data, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default value for this field, the supplied data is used
|
||||
* to source data when defaultFrom is specified.
|
||||
* @param mixed $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefaultFromData($data)
|
||||
{
|
||||
if ($this->defaultFrom) {
|
||||
return $this->getFieldNameFromData($this->defaultFrom, $data);
|
||||
}
|
||||
|
||||
if ($this->defaults !== '') {
|
||||
return $this->defaults;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the final model and attribute name of a nested attribute.
|
||||
* Eg: list($model, $attribute) = $this->resolveAttribute('person[phone]');
|
||||
* @param string $attribute.
|
||||
* @return array
|
||||
*/
|
||||
public function resolveModelAttribute($model, $attribute = null)
|
||||
{
|
||||
if ($attribute === null) {
|
||||
$attribute = $this->valueFrom ?: $this->fieldName;
|
||||
}
|
||||
|
||||
$parts = is_array($attribute) ? $attribute : HtmlHelper::nameToArray($attribute);
|
||||
$last = array_pop($parts);
|
||||
|
||||
foreach ($parts as $part) {
|
||||
$model = $model->{$part};
|
||||
}
|
||||
|
||||
return [$model, $last];
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to extract the value of a field name from a data set.
|
||||
* @param string $fieldName
|
||||
* @param mixed $data
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getFieldNameFromData($fieldName, $data, $default = null)
|
||||
{
|
||||
/*
|
||||
* Array field name, eg: field[key][key2][key3]
|
||||
*/
|
||||
|
|
@ -588,26 +641,4 @@ class FormField
|
|||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the final model and attribute name of a nested attribute.
|
||||
* Eg: list($model, $attribute) = $this->resolveAttribute('person[phone]');
|
||||
* @param string $attribute.
|
||||
* @return array
|
||||
*/
|
||||
public function resolveModelAttribute($model, $attribute = null)
|
||||
{
|
||||
if ($attribute === null) {
|
||||
$attribute = $this->valueFrom ?: $this->fieldName;
|
||||
}
|
||||
|
||||
$parts = is_array($attribute) ? $attribute : HtmlHelper::nameToArray($attribute);
|
||||
$last = array_pop($parts);
|
||||
|
||||
foreach ($parts as $part) {
|
||||
$model = $model->{$part};
|
||||
}
|
||||
|
||||
return [$model, $last];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -113,17 +113,9 @@ abstract class FormWidgetBase extends WidgetBase
|
|||
*/
|
||||
public function getLoadValue()
|
||||
{
|
||||
$defaultValue = null;
|
||||
|
||||
if (!$this->model->exists) {
|
||||
if ($this->formField->defaultFrom) {
|
||||
list($model, $attribute) = $this->formField->resolveModelAttribute($this->model, $this->formField->defaultFrom);
|
||||
$defaultValue = $model->{$attribute};
|
||||
}
|
||||
elseif ($this->formField->defaults !== '') {
|
||||
$defaultValue = $this->formField->defaults;
|
||||
}
|
||||
}
|
||||
$defaultValue = !$this->model->exists
|
||||
? $this->formField->getDefaultFromData($this->data ?: $this->model)
|
||||
: null;
|
||||
|
||||
return $this->formField->getValueFromData($this->data ?: $this->model, $defaultValue);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -775,17 +775,9 @@ class Form extends WidgetBase
|
|||
$field = $this->allFields[$field];
|
||||
}
|
||||
|
||||
$defaultValue = null;
|
||||
|
||||
if (!$this->model->exists) {
|
||||
if ($field->defaultFrom) {
|
||||
list($model, $attribute) = $field->resolveModelAttribute($this->model, $field->defaultFrom);
|
||||
$defaultValue = $model->{$attribute};
|
||||
}
|
||||
elseif ($field->defaults !== '') {
|
||||
$defaultValue = $field->defaults;
|
||||
}
|
||||
}
|
||||
$defaultValue = !$this->model->exists
|
||||
? $field->getDefaultFromData($this->data)
|
||||
: null;
|
||||
|
||||
return $field->getValueFromData($this->data, $defaultValue);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue