From c6bfd5d01ce52d794dde893cc7410d0814a6fd5e Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 10 Mar 2015 09:34:59 +1100 Subject: [PATCH] Move this logic to no longer depend on AR model --- modules/backend/classes/FormField.php | 22 ++++++++++++++++++++++ modules/backend/classes/FormWidgetBase.php | 3 +-- modules/backend/widgets/Form.php | 3 +-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 51ffd485c..2a9a2ec06 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -557,4 +557,26 @@ 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]; + } } diff --git a/modules/backend/classes/FormWidgetBase.php b/modules/backend/classes/FormWidgetBase.php index 90516423b..716b84dd0 100644 --- a/modules/backend/classes/FormWidgetBase.php +++ b/modules/backend/classes/FormWidgetBase.php @@ -125,8 +125,7 @@ abstract class FormWidgetBase extends WidgetBase */ public function resolveModelAttribute($attribute) { - $parts = HtmlHelper::nameToArray($attribute); - return $this->model->resolveAttribute($parts); + return $this->formField->resolveModelAttribute($this->model, $attribute); } } diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 39d86ca96..5d268ad50 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -913,8 +913,7 @@ class Form extends WidgetBase * Refer to the model method or any of its behaviors */ if (!is_array($fieldOptions) && !$fieldOptions) { - $htmlArray = HtmlHelper::nameToArray($field->fieldName); - list($model, $attribute) = $this->model->resolveAttribute($htmlArray); + list($model, $attribute) = $field->resolveModelAttribute($this->model, $field->fieldName); $methodName = 'get'.studly_case($attribute).'Options'; if (