Move this logic to no longer depend on AR model

This commit is contained in:
Samuel Georges 2015-03-10 09:34:59 +11:00
parent a324f01678
commit c6bfd5d01c
3 changed files with 24 additions and 4 deletions

View File

@ -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];
}
}

View File

@ -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);
}
}

View File

@ -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 (