Upgrade to rc5
This commit is contained in:
@@ -248,6 +248,26 @@ class Field extends Component
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new field instance
|
||||
*
|
||||
* @param string $type
|
||||
* @param array $attrs
|
||||
* @param Fields|null $formFields
|
||||
* @return static
|
||||
*/
|
||||
public static function factory(string $type, array $attrs = [], ?Fields $formFields = null)
|
||||
{
|
||||
$field = static::$types[$type] ?? null;
|
||||
|
||||
if (is_string($field) && class_exists($field) === true) {
|
||||
$attrs['siblings'] = $formFields;
|
||||
return new $field($attrs);
|
||||
}
|
||||
|
||||
return new static($type, $attrs, $formFields);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parent collection with all fields of the current form
|
||||
*
|
||||
@@ -404,8 +424,6 @@ class Field extends Component
|
||||
|
||||
unset($array['model']);
|
||||
|
||||
$array['errors'] = $this->errors();
|
||||
$array['invalid'] = $this->isInvalid();
|
||||
$array['saveable'] = $this->save();
|
||||
$array['signature'] = md5(json_encode($array));
|
||||
|
||||
|
265
kirby/src/Form/Field/BlocksField.php
Executable file
265
kirby/src/Form/Field/BlocksField.php
Executable file
@@ -0,0 +1,265 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Cms\Block;
|
||||
use Kirby\Cms\Blocks as BlocksCollection;
|
||||
use Kirby\Cms\Fieldsets;
|
||||
use Kirby\Cms\Form;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Exception\NotFoundException;
|
||||
use Kirby\Form\FieldClass;
|
||||
use Kirby\Form\Mixin\EmptyState;
|
||||
use Kirby\Form\Mixin\Max;
|
||||
use Kirby\Form\Mixin\Min;
|
||||
use Throwable;
|
||||
|
||||
class BlocksField extends FieldClass
|
||||
{
|
||||
use EmptyState;
|
||||
use Max;
|
||||
use Min;
|
||||
|
||||
protected $fieldsets;
|
||||
protected $blocks;
|
||||
protected $value = [];
|
||||
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
$this->setFieldsets($params['fieldsets'] ?? null, $params['model'] ?? site());
|
||||
|
||||
parent::__construct($params);
|
||||
|
||||
$this->setEmpty($params['empty'] ?? null);
|
||||
$this->setGroup($params['group'] ?? 'blocks');
|
||||
$this->setMax($params['max'] ?? null);
|
||||
$this->setMin($params['min'] ?? null);
|
||||
$this->setPretty($params['pretty'] ?? false);
|
||||
}
|
||||
|
||||
public function blocksToValues($blocks, $to = 'values'): array
|
||||
{
|
||||
$result = [];
|
||||
$fields = [];
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
try {
|
||||
$type = $block['type'];
|
||||
|
||||
// get and cache fields at the same time
|
||||
$fields[$type] = $fields[$type] ?? $this->fields($block['type']);
|
||||
|
||||
// overwrite the block content with form values
|
||||
$block['content'] = $this->form($fields[$type], $block['content'])->$to();
|
||||
|
||||
$result[] = $block;
|
||||
} catch (Throwable $e) {
|
||||
$result[] = $block;
|
||||
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function fields(string $type)
|
||||
{
|
||||
return $this->fieldset($type)->fields();
|
||||
}
|
||||
|
||||
public function fieldset(string $type)
|
||||
{
|
||||
if ($fieldset = $this->fieldsets->find($type)) {
|
||||
return $fieldset;
|
||||
}
|
||||
|
||||
throw new NotFoundException('The fieldset ' . $type . ' could not be found');
|
||||
}
|
||||
|
||||
public function fieldsets()
|
||||
{
|
||||
return $this->fieldsets;
|
||||
}
|
||||
|
||||
public function fieldsetGroups(): ?array
|
||||
{
|
||||
$fieldsetGroups = $this->fieldsets()->groups();
|
||||
return empty($fieldsetGroups) === true ? null : $fieldsetGroups;
|
||||
}
|
||||
|
||||
public function fill($value = null)
|
||||
{
|
||||
$value = BlocksCollection::parse($value);
|
||||
$blocks = BlocksCollection::factory($value);
|
||||
$this->value = $this->blocksToValues($blocks->toArray());
|
||||
}
|
||||
|
||||
public function form(array $fields, array $input = [])
|
||||
{
|
||||
return new Form([
|
||||
'fields' => $fields,
|
||||
'model' => $this->model,
|
||||
'strict' => true,
|
||||
'values' => $input,
|
||||
]);
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return count($this->value()) === 0;
|
||||
}
|
||||
|
||||
public function group(): string
|
||||
{
|
||||
return $this->group;
|
||||
}
|
||||
|
||||
public function pretty(): bool
|
||||
{
|
||||
return $this->pretty;
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
return [
|
||||
'empty' => $this->empty(),
|
||||
'fieldsets' => $this->fieldsets()->toArray(),
|
||||
'fieldsetGroups' => $this->fieldsetGroups(),
|
||||
'group' => $this->group(),
|
||||
'max' => $this->max(),
|
||||
'min' => $this->min(),
|
||||
] + parent::props();
|
||||
}
|
||||
|
||||
public function routes(): array
|
||||
{
|
||||
$field = $this;
|
||||
|
||||
return [
|
||||
[
|
||||
'pattern' => 'uuid',
|
||||
'action' => function () {
|
||||
return ['uuid' => uuid()];
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)',
|
||||
'method' => 'GET',
|
||||
'action' => function ($fieldsetType) use ($field) {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$defaults = $field->form($fields, [])->data(true);
|
||||
$content = $field->form($fields, $defaults)->values();
|
||||
|
||||
return Block::factory([
|
||||
'content' => $content,
|
||||
'type' => $fieldsetType
|
||||
])->toArray();
|
||||
}
|
||||
],
|
||||
[
|
||||
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (string $fieldsetType, string $fieldName, string $path = null) use ($field) {
|
||||
$fields = $field->fields($fieldsetType);
|
||||
$field = $field->form($fields)->field($fieldName);
|
||||
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => array_merge($this->data(), ['field' => $field])
|
||||
]);
|
||||
|
||||
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
|
||||
}
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function store($value)
|
||||
{
|
||||
$blocks = $this->blocksToValues((array)$value, 'content');
|
||||
return $this->valueToJson($blocks, $this->pretty());
|
||||
}
|
||||
|
||||
protected function setFieldsets($fieldsets, $model)
|
||||
{
|
||||
if (is_string($fieldsets) === true) {
|
||||
$fieldsets = [];
|
||||
}
|
||||
|
||||
$this->fieldsets = Fieldsets::factory($fieldsets, [
|
||||
'parent' => $model
|
||||
]);
|
||||
}
|
||||
|
||||
protected function setGroup(string $group = null)
|
||||
{
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
protected function setPretty(bool $pretty = false)
|
||||
{
|
||||
$this->pretty = $pretty;
|
||||
}
|
||||
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'blocks' => function ($value) {
|
||||
if ($this->min && count($value) < $this->min) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.min.' . ($this->min === 1 ? 'singular' : 'plural'),
|
||||
'data' => [
|
||||
'min' => $this->min
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->max && count($value) > $this->max) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.max.' . ($this->max === 1 ? 'singular' : 'plural'),
|
||||
'data' => [
|
||||
'max' => $this->max
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
$fields = [];
|
||||
$index = 0;
|
||||
|
||||
foreach ($value as $block) {
|
||||
$index++;
|
||||
$blockType = $block['type'];
|
||||
|
||||
try {
|
||||
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
|
||||
} catch (Throwable $e) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
|
||||
// store the fields for the next round
|
||||
$fields[$blockType] = $blockFields;
|
||||
|
||||
// overwrite the content with the serialized form
|
||||
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
// rough first validation
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'blocks.validation',
|
||||
'data' => [
|
||||
'index' => $index,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
224
kirby/src/Form/Field/LayoutField.php
Executable file
224
kirby/src/Form/Field/LayoutField.php
Executable file
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Form\Field;
|
||||
|
||||
use Kirby\Cms\Fieldset;
|
||||
use Kirby\Cms\Form;
|
||||
use Kirby\Cms\Layout;
|
||||
use Kirby\Cms\Layouts;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\Str;
|
||||
use Throwable;
|
||||
|
||||
class LayoutField extends BlocksField
|
||||
{
|
||||
protected $layouts;
|
||||
protected $settings;
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
$this->setModel($params['model'] ?? site());
|
||||
$this->setLayouts($params['layouts'] ?? ['1/1']);
|
||||
$this->setSettings($params['settings'] ?? []);
|
||||
|
||||
parent::__construct($params);
|
||||
}
|
||||
|
||||
public function fill($value = null)
|
||||
{
|
||||
$value = $this->valueFromJson($value);
|
||||
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
|
||||
foreach ($layouts as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$layouts[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->values();
|
||||
}
|
||||
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->value = $layouts;
|
||||
}
|
||||
|
||||
public function attrsForm(array $input = [])
|
||||
{
|
||||
$settings = $this->settings();
|
||||
|
||||
return new Form([
|
||||
'fields' => $settings ? $settings->fields() : [],
|
||||
'model' => $this->model,
|
||||
'strict' => true,
|
||||
'values' => $input,
|
||||
]);
|
||||
}
|
||||
|
||||
public function layouts(): ?array
|
||||
{
|
||||
return $this->layouts;
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
$settings = $this->settings();
|
||||
|
||||
return array_merge(parent::props(), [
|
||||
'settings' => $settings !== null ? $settings->toArray() : null,
|
||||
'layouts' => $this->layouts()
|
||||
]);
|
||||
}
|
||||
|
||||
public function routes(): array
|
||||
{
|
||||
$field = $this;
|
||||
$routes = parent::routes();
|
||||
$routes[] = [
|
||||
'pattern' => 'layout',
|
||||
'method' => 'POST',
|
||||
'action' => function () use ($field) {
|
||||
$defaults = $field->attrsForm([])->data(true);
|
||||
$attrs = $field->attrsForm($defaults)->values();
|
||||
$columns = get('columns') ?? ['1/1'];
|
||||
|
||||
return Layout::factory([
|
||||
'attrs' => $attrs,
|
||||
'columns' => array_map(function ($width) {
|
||||
return [
|
||||
'blocks' => [],
|
||||
'id' => uuid(),
|
||||
'width' => $width,
|
||||
];
|
||||
}, $columns)
|
||||
])->toArray();
|
||||
},
|
||||
];
|
||||
|
||||
$routes[] = [
|
||||
'pattern' => 'fields/(:any)/(:all?)',
|
||||
'method' => 'ALL',
|
||||
'action' => function (string $fieldName, string $path = null) use ($field) {
|
||||
$form = $field->attrsForm();
|
||||
$field = $form->field($fieldName);
|
||||
|
||||
$fieldApi = $this->clone([
|
||||
'routes' => $field->api(),
|
||||
'data' => array_merge($this->data(), ['field' => $field])
|
||||
]);
|
||||
|
||||
return $fieldApi->call($path, $this->requestMethod(), $this->requestData());
|
||||
}
|
||||
];
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
protected function setLayouts(array $layouts = [])
|
||||
{
|
||||
$this->layouts = array_map(function ($layout) {
|
||||
return Str::split($layout);
|
||||
}, $layouts);
|
||||
}
|
||||
|
||||
protected function setSettings(array $settings = [])
|
||||
{
|
||||
if (empty($settings) === true) {
|
||||
$this->settings = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$settings['icon'] = 'dashboard';
|
||||
$settings['type'] = 'layout';
|
||||
$settings['parent'] = $this->model();
|
||||
|
||||
$this->settings = Fieldset::factory($settings);
|
||||
}
|
||||
|
||||
public function settings()
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
public function store($value)
|
||||
{
|
||||
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
|
||||
|
||||
foreach ($value as $layoutIndex => $layout) {
|
||||
if ($this->settings !== null) {
|
||||
$value[$layoutIndex]['attrs'] = $this->attrsForm($layout['attrs'])->content();
|
||||
}
|
||||
|
||||
foreach ($layout['columns'] as $columnIndex => $column) {
|
||||
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->valueToJson($value, $this->pretty());
|
||||
}
|
||||
|
||||
public function validations(): array
|
||||
{
|
||||
return [
|
||||
'layout' => function ($value) {
|
||||
$fields = [];
|
||||
$layoutIndex = 0;
|
||||
|
||||
foreach ($value as $layout) {
|
||||
$layoutIndex++;
|
||||
|
||||
// validate settings form
|
||||
foreach ($this->attrsForm($layout['attrs'] ?? [])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'layout.validation.settings',
|
||||
'data' => [
|
||||
'index' => $layoutIndex
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// validate blocks in the layout
|
||||
$blockIndex = 0;
|
||||
|
||||
foreach ($layout['columns'] ?? [] as $column) {
|
||||
foreach ($column['blocks'] ?? [] as $block) {
|
||||
$blockIndex++;
|
||||
$blockType = $block['type'];
|
||||
|
||||
try {
|
||||
$blockFields = $fields[$blockType] ?? $this->fields($blockType) ?? [];
|
||||
} catch (Throwable $e) {
|
||||
// skip invalid blocks
|
||||
continue;
|
||||
}
|
||||
|
||||
// store the fields for the next round
|
||||
$fields[$blockType] = $blockFields;
|
||||
|
||||
// overwrite the content with the serialized form
|
||||
foreach ($this->form($blockFields, $block['content'])->fields() as $field) {
|
||||
$errors = $field->errors();
|
||||
|
||||
// rough first validation
|
||||
if (empty($errors) === false) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'layout.validation.block',
|
||||
'data' => [
|
||||
'blockIndex' => $blockIndex,
|
||||
'layoutIndex' => $layoutIndex
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
627
kirby/src/Form/FieldClass.php
Executable file
627
kirby/src/Form/FieldClass.php
Executable file
@@ -0,0 +1,627 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Form;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Cms\HasSiblings;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Data\Data;
|
||||
use Kirby\Toolkit\I18n;
|
||||
use Kirby\Toolkit\Str;
|
||||
use Throwable;
|
||||
|
||||
abstract class FieldClass
|
||||
{
|
||||
use HasSiblings;
|
||||
|
||||
protected $after;
|
||||
protected $autofocus;
|
||||
protected $before;
|
||||
protected $default;
|
||||
protected $disabled;
|
||||
protected $help;
|
||||
protected $icon;
|
||||
protected $label;
|
||||
/**
|
||||
* @var \Kirby\Cms\ModelWithContent
|
||||
*/
|
||||
protected $model;
|
||||
protected $name;
|
||||
protected $params;
|
||||
protected $placeholder;
|
||||
protected $required;
|
||||
protected $siblings;
|
||||
protected $translate;
|
||||
protected $value;
|
||||
protected $when;
|
||||
protected $width;
|
||||
|
||||
public function __call(string $param, array $args)
|
||||
{
|
||||
if (isset($this->$param) === true) {
|
||||
return $this->$param;
|
||||
}
|
||||
|
||||
return $this->params[$param] ?? null;
|
||||
}
|
||||
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
$this->params = $params;
|
||||
|
||||
$this->setAfter($params['after'] ?? null);
|
||||
$this->setAutofocus($params['autofocus'] ?? false);
|
||||
$this->setBefore($params['before'] ?? null);
|
||||
$this->setDefault($params['default'] ?? null);
|
||||
$this->setDisabled($params['disabled'] ?? false);
|
||||
$this->setHelp($params['help'] ?? null);
|
||||
$this->setIcon($params['icon'] ?? null);
|
||||
$this->setLabel($params['label'] ?? null);
|
||||
$this->setModel($params['model'] ?? site());
|
||||
$this->setName($params['name'] ?? null);
|
||||
$this->setPlaceholder($params['placeholder'] ?? null);
|
||||
$this->setRequired($params['required'] ?? false);
|
||||
$this->setSiblings($params['siblings'] ?? null);
|
||||
$this->setTranslate($params['translate'] ?? true);
|
||||
$this->setWhen($params['when'] ?? null);
|
||||
$this->setWidth($params['width'] ?? null);
|
||||
|
||||
if (array_key_exists('value', $params) === true) {
|
||||
$this->fill($params['value']);
|
||||
}
|
||||
}
|
||||
|
||||
public function after(): ?string
|
||||
{
|
||||
return $this->stringTemplate($this->after);
|
||||
}
|
||||
|
||||
public function api(): array
|
||||
{
|
||||
return $this->routes();
|
||||
}
|
||||
|
||||
public function autofocus(): bool
|
||||
{
|
||||
return $this->autofocus;
|
||||
}
|
||||
|
||||
public function before(): ?string
|
||||
{
|
||||
return $this->stringTemplate($this->before);
|
||||
}
|
||||
|
||||
/**
|
||||
* DEPRECATED!
|
||||
*
|
||||
* Returns the field data
|
||||
* in a format to be stored
|
||||
* in Kirby's content fields
|
||||
*
|
||||
* @param bool $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function data(bool $default = false)
|
||||
{
|
||||
return $this->store($this->value($default));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default value for the field,
|
||||
* which will be used when a page/file/user is created
|
||||
*/
|
||||
public function default()
|
||||
{
|
||||
if ($this->default === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_string($this->default) === false) {
|
||||
return $this->default;
|
||||
}
|
||||
|
||||
return $this->stringTemplate($this->default);
|
||||
}
|
||||
|
||||
/**
|
||||
* If `true`, the field is no longer editable and will not be saved
|
||||
*/
|
||||
public function disabled(): bool
|
||||
{
|
||||
return $this->disabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional help text below the field
|
||||
*/
|
||||
public function help(): ?string
|
||||
{
|
||||
if (empty($this->help) === false) {
|
||||
$help = $this->stringTemplate($this->help);
|
||||
$help = $this->kirby()->kirbytext($help);
|
||||
return $help;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function i18n($param)
|
||||
{
|
||||
return empty($param) === false ? I18n::translate($param, $param) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional icon that will be shown at the end of the field
|
||||
*/
|
||||
public function icon(): ?string
|
||||
{
|
||||
return $this->icon;
|
||||
}
|
||||
|
||||
public function id(): string
|
||||
{
|
||||
return $this->name();
|
||||
}
|
||||
|
||||
public function isDisabled(): bool
|
||||
{
|
||||
return $this->disabled;
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return $this->isEmptyValue($this->value());
|
||||
}
|
||||
|
||||
public function isEmptyValue($value): bool
|
||||
{
|
||||
return in_array($value, [null, '', []], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the field is invalid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isInvalid(): bool
|
||||
{
|
||||
return $this->isValid() === false;
|
||||
}
|
||||
|
||||
public function isRequired(): bool
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
public function isSaveable(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the field is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(): bool
|
||||
{
|
||||
return empty($this->errors()) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs all validations and returns an array of
|
||||
* error messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function errors(): array
|
||||
{
|
||||
return $this->validate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the value
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function fill($value = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Kirby instance
|
||||
*
|
||||
* @return \Kirby\Cms\App
|
||||
*/
|
||||
public function kirby()
|
||||
{
|
||||
return $this->model->kirby();
|
||||
}
|
||||
|
||||
/**
|
||||
* The field label can be set as string or associative array with translations
|
||||
*/
|
||||
public function label(): string
|
||||
{
|
||||
return $this->stringTemplate($this->label ?? Str::ucfirst($this->name()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parent model
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function model()
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the field name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name ?? $this->type();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the field needs a value before being saved;
|
||||
* this is the case if all of the following requirements are met:
|
||||
* - The field is saveable
|
||||
* - The field is required
|
||||
* - The field is currently empty
|
||||
* - The field is not currently inactive because of a `when` rule
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function needsValue(): bool
|
||||
{
|
||||
// check simple conditions first
|
||||
if (
|
||||
$this->isSaveable() === false ||
|
||||
$this->isRequired() === false ||
|
||||
$this->isEmpty() === false
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check the data of the relevant fields if there is a `when` option
|
||||
if (empty($this->when) === false && is_array($this->when) === true) {
|
||||
$formFields = $this->siblings();
|
||||
|
||||
if ($formFields !== null) {
|
||||
foreach ($this->when as $field => $value) {
|
||||
$field = $formFields->get($field);
|
||||
$inputValue = $field !== null ? $field->value() : '';
|
||||
|
||||
// if the input data doesn't match the requested `when` value,
|
||||
// that means that this field is not required and can be saved
|
||||
// (*all* `when` conditions must be met for this field to be required)
|
||||
if ($inputValue !== $value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// either there was no `when` condition or all conditions matched
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all original params for the field
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function params(): array
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional placeholder value that will be shown when the field is empty
|
||||
*/
|
||||
public function placeholder(): ?string
|
||||
{
|
||||
return $this->stringTemplate($this->placeholder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the props that will be sent to
|
||||
* the Vue component
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function props(): array
|
||||
{
|
||||
return [
|
||||
'after' => $this->after(),
|
||||
'autofocus' => $this->autofocus(),
|
||||
'before' => $this->before(),
|
||||
'default' => $this->default(),
|
||||
'disabled' => $this->isDisabled(),
|
||||
'help' => $this->help(),
|
||||
'icon' => $this->icon(),
|
||||
'label' => $this->label(),
|
||||
'name' => $this->name(),
|
||||
'placeholder' => $this->placeholder(),
|
||||
'required' => $this->isRequired(),
|
||||
'saveable' => $this->isSaveable(),
|
||||
'translate' => $this->translate(),
|
||||
'type' => $this->type(),
|
||||
'width' => $this->width(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* If `true`, the field has to be filled in correctly to be saved.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function required(): bool
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
/**
|
||||
* Routes for the field API
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function routes(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* DEPRECATED
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
return $this->isSaveable();
|
||||
}
|
||||
|
||||
protected function setAfter($after = null)
|
||||
{
|
||||
$this->after = $this->i18n($after);
|
||||
}
|
||||
|
||||
protected function setAutofocus(bool $autofocus = false)
|
||||
{
|
||||
$this->autofocus = $autofocus;
|
||||
}
|
||||
|
||||
protected function setBefore($before = null)
|
||||
{
|
||||
$this->before = $this->i18n($before);
|
||||
}
|
||||
|
||||
protected function setDefault($default = null)
|
||||
{
|
||||
$this->default = $default;
|
||||
}
|
||||
|
||||
protected function setDisabled(bool $disabled = false)
|
||||
{
|
||||
$this->disabled = $disabled;
|
||||
}
|
||||
|
||||
protected function setHelp($help = null)
|
||||
{
|
||||
$this->help = $this->i18n($help);
|
||||
}
|
||||
|
||||
protected function setIcon(string $icon = null)
|
||||
{
|
||||
$this->icon = $icon;
|
||||
}
|
||||
|
||||
protected function setLabel($label = null)
|
||||
{
|
||||
$this->label = $this->i18n($label);
|
||||
}
|
||||
|
||||
protected function setModel(ModelWithContent $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
protected function setName(string $name = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
protected function setPlaceholder($placeholder = null)
|
||||
{
|
||||
$this->placeholder = $this->i18n($placeholder);
|
||||
}
|
||||
|
||||
protected function setRequired(bool $required = false)
|
||||
{
|
||||
$this->required = $required;
|
||||
}
|
||||
|
||||
protected function setSiblings(Fields $siblings = null)
|
||||
{
|
||||
$this->siblings = $siblings ?? new Fields([]);
|
||||
}
|
||||
|
||||
protected function setTranslate(bool $translate = true)
|
||||
{
|
||||
$this->translate = $translate;
|
||||
}
|
||||
|
||||
protected function setWhen($when = null)
|
||||
{
|
||||
$this->when = $when;
|
||||
}
|
||||
|
||||
protected function setWidth(string $width = null)
|
||||
{
|
||||
$this->width = $width;
|
||||
}
|
||||
|
||||
protected function siblingsCollection()
|
||||
{
|
||||
return $this->siblings;
|
||||
}
|
||||
|
||||
protected function stringTemplate(?string $string = null): ?string
|
||||
{
|
||||
if ($string !== null) {
|
||||
return $this->model->toString($string);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function store($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the field be translatable?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function translate(): bool
|
||||
{
|
||||
return $this->translate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the field to a plain array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$props = $this->props();
|
||||
$props['signature'] = md5(json_encode($props));
|
||||
|
||||
ksort($props);
|
||||
|
||||
return array_filter($props, function ($item) {
|
||||
return $item !== null;
|
||||
});
|
||||
}
|
||||
|
||||
public function type(): string
|
||||
{
|
||||
return lcfirst(basename(str_replace(['\\', 'Field'], ['/', ''], static::class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the validations defined for the field
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function validate(): array
|
||||
{
|
||||
$validations = $this->validations();
|
||||
$value = $this->value();
|
||||
$errors = [];
|
||||
|
||||
// validate required values
|
||||
if ($this->needsValue() === true) {
|
||||
$errors['required'] = I18n::translate('error.validation.required');
|
||||
}
|
||||
|
||||
foreach ($validations as $key => $validation) {
|
||||
if (is_int($key) === true) {
|
||||
// predefined validation
|
||||
try {
|
||||
Validations::$validation($this, $value);
|
||||
} catch (Exception $e) {
|
||||
$errors[$validation] = $e->getMessage();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_a($validation, 'Closure') === true) {
|
||||
try {
|
||||
$validation->call($this, $value);
|
||||
} catch (Exception $e) {
|
||||
$errors[$key] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines all validation rules
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function validations(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the field if saveable
|
||||
* otherwise it returns null
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function value(bool $default = false)
|
||||
{
|
||||
if ($this->isSaveable() === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($default === true && $this->isEmpty() === true) {
|
||||
return $this->default();
|
||||
}
|
||||
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
protected function valueFromJson($value): array
|
||||
{
|
||||
try {
|
||||
return Data::decode($value, 'json');
|
||||
} catch (Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
protected function valueFromYaml($value)
|
||||
{
|
||||
return Data::decode($value, 'yaml');
|
||||
}
|
||||
|
||||
protected function valueToJson(array $value = null, bool $pretty = false): string
|
||||
{
|
||||
if ($pretty === true) {
|
||||
return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
return json_encode($value);
|
||||
}
|
||||
|
||||
protected function valueToYaml(array $value = null): string
|
||||
{
|
||||
return Data::encode($value, 'yaml');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the width of the field in
|
||||
* the Panel grid
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function width(): string
|
||||
{
|
||||
return $this->width ?? '1/1';
|
||||
}
|
||||
}
|
@@ -30,7 +30,7 @@ class Fields extends Collection
|
||||
if (is_array($field) === true) {
|
||||
// use the array key as name if the name is not set
|
||||
$field['name'] = $field['name'] ?? $name;
|
||||
$field = new Field($field['type'], $field);
|
||||
$field = Field::factory($field['type'], $field, $this);
|
||||
}
|
||||
|
||||
return parent::__set($field->name(), $field);
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Kirby\Form;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Data\Data;
|
||||
use Throwable;
|
||||
|
||||
@@ -81,7 +82,7 @@ class Form
|
||||
}
|
||||
|
||||
try {
|
||||
$field = new Field($props['type'], $props, $this->fields);
|
||||
$field = Field::factory($props['type'], $props, $this->fields);
|
||||
} catch (Throwable $e) {
|
||||
$field = static::exceptionField($e, $props);
|
||||
}
|
||||
@@ -178,13 +179,19 @@ class Form
|
||||
*/
|
||||
public static function exceptionField(Throwable $exception, array $props = [])
|
||||
{
|
||||
$message = $exception->getMessage();
|
||||
|
||||
if (App::instance()->option('debug') === true) {
|
||||
$message .= ' in file: ' . $exception->getFile() . ' line: ' . $exception->getLine();
|
||||
}
|
||||
|
||||
$props = array_merge($props, [
|
||||
'label' => 'Error in "' . $props['name'] . '" field',
|
||||
'label' => 'Error in "' . $props['name'] . '" field.',
|
||||
'theme' => 'negative',
|
||||
'text' => strip_tags($exception->getMessage()),
|
||||
'text' => strip_tags($message),
|
||||
]);
|
||||
|
||||
return new Field('info', $props);
|
||||
return Field::factory('info', $props);
|
||||
}
|
||||
|
||||
/**
|
||||
|
18
kirby/src/Form/Mixin/EmptyState.php
Executable file
18
kirby/src/Form/Mixin/EmptyState.php
Executable file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Form\Mixin;
|
||||
|
||||
trait EmptyState
|
||||
{
|
||||
protected $empty;
|
||||
|
||||
protected function setEmpty($empty = null)
|
||||
{
|
||||
$this->empty = $this->i18n($empty);
|
||||
}
|
||||
|
||||
public function empty(): ?string
|
||||
{
|
||||
return $this->stringTemplate($this->empty);
|
||||
}
|
||||
}
|
18
kirby/src/Form/Mixin/Max.php
Executable file
18
kirby/src/Form/Mixin/Max.php
Executable file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Form\Mixin;
|
||||
|
||||
trait Max
|
||||
{
|
||||
protected $max;
|
||||
|
||||
public function max(): ?int
|
||||
{
|
||||
return $this->max;
|
||||
}
|
||||
|
||||
protected function setMax(int $max = null)
|
||||
{
|
||||
$this->max = $max;
|
||||
}
|
||||
}
|
18
kirby/src/Form/Mixin/Min.php
Executable file
18
kirby/src/Form/Mixin/Min.php
Executable file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Form\Mixin;
|
||||
|
||||
trait Min
|
||||
{
|
||||
protected $min;
|
||||
|
||||
public function min(): ?int
|
||||
{
|
||||
return $this->min;
|
||||
}
|
||||
|
||||
protected function setMin(int $min = null)
|
||||
{
|
||||
$this->min = $min;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user