!!! *BREAKING*: Form widget base class no longer takes a model as the 2nd argument, it should be passed as `model` in the configuration instead.

This commit is contained in:
Samuel Georges 2015-03-06 20:37:05 +11:00
parent 2561131f38
commit 507bfab768
5 changed files with 57 additions and 31 deletions

View File

@ -1,3 +1,8 @@
* **Build 217** (2015-03-06)
- Added new form field widget called `repeater` for repeatable fields (see Backend > Forms docs).
- Made some UI improvements to the Rich Editor.
- Form widget base class no longer takes a model as the 2nd argument, it should be passed as `model` in the configuration instead.
* **Build 214** (2015-03-03)
- Introduce `AjaxException` that allows response contents to be sent to framework.js and still be treated as an error.

View File

@ -11,26 +11,21 @@ use Str;
*/
abstract class FormWidgetBase extends WidgetBase
{
/**
* @var FormField Object containing general form field information.
*/
public $formField;
//
// Configurable properties
//
/**
* @var string Form field name.
*/
public $fieldName;
/**
* @var string Model attribute to get/set value from.
*/
public $valueFrom;
/**
* @var mixed Model object.
* @var Model Form model object.
*/
public $model;
/**
* @var array Dataset containing field values, if none supplied model should be used.
*/
public $data;
/**
* @var string Active session key, used for editing forms and deferred bindings.
*/
@ -46,6 +41,25 @@ abstract class FormWidgetBase extends WidgetBase
*/
public $showLabels = true;
//
// Object properties
//
/**
* @var FormField Object containing general form field information.
*/
protected $formField;
/**
* @var string Form field name.
*/
protected $fieldName;
/**
* @var string Model attribute to get/set value from.
*/
protected $valueFrom;
/**
* Constructor
* @param $controller Controller Active controller object.
@ -53,19 +67,21 @@ abstract class FormWidgetBase extends WidgetBase
* @param $formField FormField Object containing general form field information.
* @param $configuration array Configuration the relates to this widget.
*/
public function __construct($controller, $model, $formField, $configuration = [])
public function __construct($controller, $formField, $configuration = [])
{
$this->formField = $formField;
$this->fieldName = $formField->fieldName;
$this->valueFrom = $formField->valueFrom;
$this->model = $model;
if (isset($configuration->sessionKey)) {
$this->sessionKey = $configuration->sessionKey;
}
if (isset($configuration->previewMode)) {
$this->previewMode = $configuration->previewMode;
}
$this->config = $this->makeConfig($configuration);
$this->fillFromConfig([
'model',
'data',
'sessionKey',
'previewMode',
'showLabels'
]);
parent::__construct($controller, $configuration);
}
@ -97,7 +113,7 @@ abstract class FormWidgetBase extends WidgetBase
*/
public function getLoadValue()
{
return $this->formField->getValueFromData($this->model);
return $this->formField->getValueFromData($this->data ?: $this->model);
}
/**

View File

@ -52,14 +52,13 @@ abstract class WidgetBase
$this->assetPath = $this->guessViewPath('/assets', true);
/*
* Apply configuration values to a new config object.
* Apply configuration values to a new config object, if a parent
* consutrctor hasn't done it already.
*/
if (!$configuration) {
$configuration = [];
if ($this->config === null) {
$this->config = $this->makeConfig($configuration);
}
$this->config = $this->makeConfig($configuration);
/*
* If no alias is set by the configuration.
*/

View File

@ -679,6 +679,9 @@ class Form extends WidgetBase
$widgetConfig = $this->makeConfig($field->config);
$widgetConfig->alias = $this->alias . studly_case(Str::evalHtmlId($field->fieldName));
$widgetConfig->sessionKey = $this->getSessionKey();
$widgetConfig->previewMode = $this->previewMode;
$widgetConfig->model = $this->model;
$widgetConfig->data = $this->data;
$widgetName = $widgetConfig->widget;
$widgetClass = $this->widgetManager->resolveFormWidget($widgetName);
@ -689,8 +692,7 @@ class Form extends WidgetBase
));
}
$widget = new $widgetClass($this->controller, $this->model, $field, $widgetConfig);
$widget->previewMode = $this->previewMode;
$widget = new $widgetClass($this->controller, $field, $widgetConfig);
return $this->formWidgets[$field->fieldName] = $widget;
}

View File

@ -28,6 +28,10 @@ trait ConfigMaker
*/
public function makeConfig($configFile = [], $requiredConfig = [])
{
if (!$configFile) {
$configFile = [];
}
/*
* Config already made
*/