Refactor how the Form widget manages fields.
Widgets no longer need to pass configuration through "options".
This commit is contained in:
parent
6d826b0732
commit
294961cf8a
|
|
@ -11,7 +11,6 @@ use Str;
|
|||
*/
|
||||
class FormField
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string Form field column name.
|
||||
*/
|
||||
|
|
@ -49,7 +48,7 @@ class FormField
|
|||
public $type = 'text';
|
||||
|
||||
/**
|
||||
* @var string Field or widget options.
|
||||
* @var string Field options.
|
||||
*/
|
||||
public $options = [];
|
||||
|
||||
|
|
@ -123,6 +122,11 @@ class FormField
|
|||
*/
|
||||
public $path;
|
||||
|
||||
/**
|
||||
* @var array Raw field configuration.
|
||||
*/
|
||||
public $config;
|
||||
|
||||
public function __construct($columnName, $label)
|
||||
{
|
||||
$this->columnName = $columnName;
|
||||
|
|
@ -158,6 +162,17 @@ class FormField
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets field options, for dropdowns, radio lists and checkbox lists.
|
||||
* @param array $value
|
||||
* @return self
|
||||
*/
|
||||
public function options($value = [])
|
||||
{
|
||||
$this->options = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies a field control rendering mode. Supported modes are:
|
||||
* - text - creates a text field. Default for varchar column types.
|
||||
|
|
@ -167,15 +182,41 @@ class FormField
|
|||
* - checkbox - creates a single checkbox.
|
||||
* - checkboxlist - creates a checkbox list.
|
||||
* @param string $type Specifies a render mode as described above
|
||||
* @param array $options A list of render mode specific options.
|
||||
* @param array $config A list of render mode specific config.
|
||||
*/
|
||||
public function displayAs($type, $options = [])
|
||||
public function displayAs($type, $config = [])
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->options = $options;
|
||||
$this->type = strtolower($type) ?: $this->type;
|
||||
$this->config = $this->evalConfig($config);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process options and apply them to this object.
|
||||
* @param array $config
|
||||
* @return array
|
||||
*/
|
||||
protected function evalConfig($config)
|
||||
{
|
||||
if (isset($config['options'])) $this->options($config['options']);
|
||||
if (isset($config['span'])) $this->span($config['span']);
|
||||
if (isset($config['context'])) $this->context = $config['context'];
|
||||
if (isset($config['size'])) $this->size($config['size']);
|
||||
if (isset($config['tab'])) $this->tab($config['tab']);
|
||||
if (isset($config['commentAbove'])) $this->comment($config['commentAbove'], 'above');
|
||||
if (isset($config['comment'])) $this->comment($config['comment']);
|
||||
if (isset($config['placeholder'])) $this->placeholder = $config['placeholder'];
|
||||
if (isset($config['default'])) $this->defaults = $config['default'];
|
||||
if (isset($config['cssClass'])) $this->cssClass = $config['cssClass'];
|
||||
if (isset($config['attributes'])) $this->attributes = $config['attributes'];
|
||||
if (isset($config['path'])) $this->path = $config['path'];
|
||||
|
||||
if (array_key_exists('required', $config)) $this->required = $config['required'];
|
||||
if (array_key_exists('disabled', $config)) $this->disabled = $config['disabled'];
|
||||
if (array_key_exists('stretch', $config)) $this->stretch = $config['stretch'];
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a text comment above or below the field.
|
||||
* @param string $text Specifies a comment text.
|
||||
|
|
@ -222,5 +263,4 @@ class FormField
|
|||
$id = rtrim(str_replace(['[', ']'], '-', $id), '-');
|
||||
return $id;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -50,6 +50,12 @@ abstract class FormWidgetBase extends WidgetBase
|
|||
if (isset($configuration->sessionKey)) $this->sessionKey = $configuration->sessionKey;
|
||||
if (isset($configuration->previewMode)) $this->previewMode = $configuration->previewMode;
|
||||
|
||||
/*
|
||||
* Form fields originally passed their configuration via the options index.
|
||||
* This step should be removed if year >= 2015.
|
||||
*/
|
||||
if (isset($configuration->options)) $configuration = array_merge($configuration->options, (array)$configuration);
|
||||
|
||||
parent::__construct($controller, $configuration);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -65,11 +65,7 @@ abstract class WidgetBase
|
|||
if (!$configuration)
|
||||
$configuration = [];
|
||||
|
||||
$this->config = new stdClass();
|
||||
foreach ($configuration as $name => $value) {
|
||||
$name = camel_case($name);
|
||||
$this->config->{$name} = $value;
|
||||
}
|
||||
$this->config = $this->makeConfig($configuration);
|
||||
|
||||
/*
|
||||
* If no alias is set by the configuration.
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
use Backend\Classes\FormWidgetBase;
|
||||
|
||||
/**
|
||||
* Code Editor
|
||||
* Renders a code editor field.
|
||||
* Date picker
|
||||
* Renders a date picker field.
|
||||
*
|
||||
* @package october\backend
|
||||
* @author Alexey Bobkov, Samuel Georges
|
||||
|
|
|
|||
|
|
@ -361,45 +361,34 @@ class Form extends WidgetBase
|
|||
$field->idPrefix = $this->getId();
|
||||
|
||||
/*
|
||||
* Simple widget field, only widget type is supplied
|
||||
* Simple field type
|
||||
*/
|
||||
if (is_string($config) && $this->isFormWidget($config) !== false) {
|
||||
$field->displayAs('widget', ['widget' => $config]);
|
||||
return $field;
|
||||
}
|
||||
/*
|
||||
* Simple field, only field type is supplied
|
||||
*/
|
||||
elseif (is_string($config)) {
|
||||
$field->displayAs($config);
|
||||
return $field;
|
||||
}
|
||||
if (is_string($config)) {
|
||||
|
||||
if ($this->isFormWidget($config) !== false)
|
||||
$field->displayAs('widget', ['widget' => $config]);
|
||||
else
|
||||
$field->displayAs($config);
|
||||
}
|
||||
/*
|
||||
* Defined field type
|
||||
*/
|
||||
$fieldType = isset($config['type']) ? $config['type'] : null;
|
||||
if (!is_string($fieldType) && !is_null($fieldType))
|
||||
throw new ApplicationException(Lang::get('backend::lang.field.invalid_type', ['type'=>gettype($fieldType)]));
|
||||
else {
|
||||
|
||||
/*
|
||||
* Process basic options
|
||||
*/
|
||||
if (isset($config['span'])) $field->span($config['span']);
|
||||
if (isset($config['context'])) $field->context = $config['context'];
|
||||
if (isset($config['size'])) $field->size($config['size']);
|
||||
if (isset($config['tab'])) $field->tab($config['tab']);
|
||||
if (isset($config['commentAbove'])) $field->comment($config['commentAbove'], 'above');
|
||||
if (isset($config['comment'])) $field->comment($config['comment']);
|
||||
if (isset($config['placeholder'])) $field->placeholder = $config['placeholder'];
|
||||
if (isset($config['default'])) $field->defaults = $config['default'];
|
||||
if (isset($config['cssClass'])) $field->cssClass = $config['cssClass'];
|
||||
if (isset($config['attributes'])) $field->attributes = $config['attributes'];
|
||||
if (isset($config['path'])) $field->path = $config['path'];
|
||||
$fieldType = isset($config['type']) ? $config['type'] : null;
|
||||
if (!is_string($fieldType) && !is_null($fieldType))
|
||||
throw new ApplicationException(Lang::get('backend::lang.field.invalid_type', ['type'=>gettype($fieldType)]));
|
||||
|
||||
if (array_key_exists('required', $config)) $field->required = $config['required'];
|
||||
if (array_key_exists('disabled', $config)) $field->disabled = $config['disabled'];
|
||||
if (array_key_exists('stretch', $config)) $field->stretch = $config['stretch'];
|
||||
/*
|
||||
* Widget with configuration
|
||||
*/
|
||||
if ($this->isFormWidget($fieldType) !== false) {
|
||||
$config['widget'] = $fieldType;
|
||||
$fieldType = 'widget';
|
||||
}
|
||||
|
||||
$field->displayAs($fieldType, $config);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set field value
|
||||
|
|
@ -407,24 +396,13 @@ class Form extends WidgetBase
|
|||
$field->value = $this->getFieldValue($field);
|
||||
|
||||
/*
|
||||
* Widget with options
|
||||
* Get field options from model
|
||||
*/
|
||||
if ($this->isFormWidget($fieldType) !== false) {
|
||||
$fieldOptions = (isset($config['options'])) ? $config['options'] : [];
|
||||
$fieldOptions['widget'] = $fieldType;
|
||||
$field->displayAs('widget', $fieldOptions);
|
||||
}
|
||||
/*
|
||||
* Simple field with options
|
||||
*/
|
||||
elseif (strlen($fieldType)) {
|
||||
$optionModelTypes = ['dropdown', 'radio', 'checkboxlist'];
|
||||
if (in_array($field->type, $optionModelTypes)) {
|
||||
$fieldOptions = (isset($config['options'])) ? $config['options'] : null;
|
||||
$studlyField = studly_case(strtolower($fieldType));
|
||||
|
||||
if (method_exists($this, 'eval'.$studlyField.'Options'))
|
||||
$fieldOptions = $this->{'eval'.$studlyField.'Options'}($field, $fieldOptions);
|
||||
|
||||
$field->displayAs($fieldType, $fieldOptions);
|
||||
$fieldOptions = $this->getOptionsFromModel($field, $fieldOptions);
|
||||
$field->options($fieldOptions);
|
||||
}
|
||||
|
||||
return $field;
|
||||
|
|
@ -460,7 +438,7 @@ class Form extends WidgetBase
|
|||
if (isset($this->formWidgets[$field->columnName]))
|
||||
return $this->formWidgets[$field->columnName];
|
||||
|
||||
$widgetConfig = $this->makeConfig($field->options);
|
||||
$widgetConfig = $this->makeConfig($field->config);
|
||||
$widgetConfig->alias = $this->alias . studly_case($field->columnName);
|
||||
$widgetConfig->sessionKey = $this->getSessionKey();
|
||||
|
||||
|
|
@ -583,30 +561,6 @@ class Form extends WidgetBase
|
|||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate and validate dropdown field options.
|
||||
*/
|
||||
public function evalDropdownOptions($field, $fieldOptions)
|
||||
{
|
||||
return $this->getOptionsFromModel($field, $fieldOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate and validate radio field options.
|
||||
*/
|
||||
public function evalRadioOptions($field, $fieldOptions)
|
||||
{
|
||||
return $this->getOptionsFromModel($field, $fieldOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate and validate checkbox list field options.
|
||||
*/
|
||||
public function evalCheckboxlistOptions($field, $fieldOptions)
|
||||
{
|
||||
return $this->getOptionsFromModel($field, $fieldOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks at the model for defined options.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue