initial commit

This commit is contained in:
isUnknown 2026-01-13 10:21:41 +01:00
commit 5210d78d7d
969 changed files with 223828 additions and 0 deletions

449
kirby/src/Form/Field.php Normal file
View file

@ -0,0 +1,449 @@
<?php
namespace Kirby\Form;
use Closure;
use Kirby\Cms\HasSiblings;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\Component;
use Kirby\Toolkit\I18n;
/**
* Form Field object that takes a Vue component style
* array of properties and methods and converts them
* to a usable field option array for the API.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @use \Kirby\Cms\HasSiblings<\Kirby\Form\Fields>
*/
class Field extends Component
{
use HasSiblings;
use Mixin\Api;
use Mixin\Model;
use Mixin\Translatable;
use Mixin\Validation;
use Mixin\When;
use Mixin\Value {
isEmptyValue as protected isEmptyValueFromMixin;
}
/**
* Parent collection with all fields of the current form
*/
protected Fields $siblings;
/**
* Registry for all component mixins
*/
public static array $mixins = [];
/**
* Registry for all component types
*/
public static array $types = [];
/**
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function __construct(
string $type,
array $attrs = [],
Fields|null $siblings = null
) {
if (isset(static::$types[$type]) === false) {
throw new InvalidArgumentException(
key: 'field.type.missing',
data: [
'name' => $attrs['name'] ?? '-',
'type' => $type
]
);
}
// use the type as fallback for the name
$attrs['name'] ??= $type;
$attrs['type'] = $type;
// set the name to lowercase
$attrs['name'] = strtolower($attrs['name']);
$this->setModel($attrs['model'] ?? null);
parent::__construct($type, $attrs);
// set the siblings collection
$this->siblings = $siblings ?? new Fields([$this]);
}
/**
* Default props and computed of the field
*/
public static function defaults(): array
{
return [
'props' => [
/**
* Optional text that will be shown after the input
*/
'after' => function ($after = null) {
return I18n::translate($after, $after);
},
/**
* Sets the focus on this field when the form loads. Only the first field with this label gets
*/
'autofocus' => function (bool|null $autofocus = null): bool {
return $autofocus ?? false;
},
/**
* Optional text that will be shown before the input
*/
'before' => function ($before = null) {
return I18n::translate($before, $before);
},
/**
* Default value for the field, which will be used when a page/file/user is created
*/
'default' => function ($default = null) {
return $default;
},
/**
* If `true`, the field is no longer editable and will not be saved
*/
'disabled' => function (bool|null $disabled = null): bool {
return $disabled ?? false;
},
/**
* Optional help text below the field
*/
'help' => function ($help = null) {
return I18n::translate($help, $help);
},
/**
* Optional icon that will be shown at the end of the field
*/
'icon' => function (string|null $icon = null) {
return $icon;
},
/**
* The field label can be set as string or associative array with translations
*/
'label' => function ($label = null) {
return I18n::translate($label, $label);
},
/**
* Optional placeholder value that will be shown when the field is empty
*/
'placeholder' => function ($placeholder = null) {
return I18n::translate($placeholder, $placeholder);
},
/**
* If `true`, the field has to be filled in correctly to be saved.
*/
'required' => function (bool|null $required = null): bool {
return $required ?? false;
},
/**
* If `false`, the field will be disabled in non-default languages and cannot be translated. This is only relevant in multi-language setups.
*/
'translate' => function (bool $translate = true): bool {
return $translate;
},
/**
* Conditions when the field will be shown (since 3.1.0)
*/
'when' => function ($when = null) {
return $when;
},
/**
* The width of the field in the field grid, e.g. `1/1`, `1/2`, `1/3`, `1/4`, `2/3`, `3/4`
*/
'width' => function (string $width = '1/1') {
return $width;
},
'value' => function ($value = null) {
return $value;
}
],
'computed' => [
'after' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->after !== null) {
return $this->model()->toString($this->after);
}
},
'before' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->before !== null) {
return $this->model()->toString($this->before);
}
},
'default' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->default === null) {
return;
}
if (is_string($this->default) === false) {
return $this->default;
}
return $this->model()->toString($this->default);
},
'help' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->help) {
$help = $this->model()->toSafeString($this->help);
$help = $this->kirby()->kirbytext($help);
return $help;
}
},
'label' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->label !== null) {
return $this->model()->toString($this->label);
}
},
'placeholder' => function () {
/** @var \Kirby\Form\Field $this */
if ($this->placeholder !== null) {
return $this->model()->toString($this->placeholder);
}
}
]
];
}
/**
* Returns optional dialog routes for the field
*/
public function dialogs(): array
{
if (
isset($this->options['dialogs']) === true &&
$this->options['dialogs'] instanceof Closure
) {
return $this->options['dialogs']->call($this);
}
return [];
}
/**
* Returns optional drawer routes for the field
*/
public function drawers(): array
{
if (
isset($this->options['drawers']) === true &&
$this->options['drawers'] instanceof Closure
) {
return $this->options['drawers']->call($this);
}
return [];
}
/**
* Returns the preferred empty state for the field
*
* @since 5.2.0
*/
public function emptyValue(): mixed
{
if (isset($this->methods['emptyValue']) === true) {
return $this->methods['emptyValue']->call($this);
}
return null;
}
/**
* Creates a new field instance
*/
public static function factory(
string $type,
array $attrs = [],
Fields|null $siblings = null
): static|FieldClass {
$field = static::$types[$type] ?? null;
if (is_string($field) && class_exists($field) === true) {
$attrs['siblings'] = $siblings;
return new $field($attrs);
}
return new static($type, $attrs, $siblings);
}
/**
* Sets a new value for the field
*/
public function fill(mixed $value): static
{
// remember the current state to restore it afterwards
$attrs = $this->attrs;
$methods = $this->methods;
$options = $this->options;
$type = $this->type;
// overwrite the attribute value
$this->value = $this->attrs['value'] = $value;
// reevaluate the value prop
$this->applyProp('value', $this->options['props']['value'] ?? $value);
// reevaluate the computed props
$this->applyComputed($this->options['computed'] ?? []);
// restore the original state
$this->attrs = $attrs;
$this->methods = $methods;
$this->options = $options;
$this->type = $type;
return $this;
}
/**
* Preferred name would be `::reset` but this is
* taken by options in other fields.
*
* @since 5.2.0
*/
public function fillWithEmptyValue(): static
{
$this->value = $this->emptyValue();
return $this;
}
/**
* @deprecated 5.0.0 Use `::siblings() instead
*/
public function formFields(): Fields
{
return $this->siblings;
}
/**
* Checks if the field has a value
*/
public function hasValue(): bool
{
return ($this->options['save'] ?? true) !== false;
}
/**
* Checks if the field is disabled
*/
public function isDisabled(): bool
{
return $this->disabled === true;
}
/**
* Checks if the given value is considered empty
*/
public function isEmptyValue(mixed $value = null): bool
{
if (
isset($this->options['isEmpty']) === true &&
$this->options['isEmpty'] instanceof Closure
) {
return $this->options['isEmpty']->call($this, $value);
}
return $this->isEmptyValueFromMixin($value);
}
/**
* Checks if the field is hidden
*/
public function isHidden(): bool
{
return ($this->options['hidden'] ?? false) === true;
}
/**
* Returns field api routes
*/
public function routes(): array
{
if (
isset($this->options['api']) === true &&
$this->options['api'] instanceof Closure
) {
return $this->options['api']->call($this);
}
return [];
}
/**
* Parent collection with all fields of the current form
*/
public function siblings(): Fields
{
return $this->siblings;
}
/**
* Returns all sibling fields for the HasSiblings trait
*/
protected function siblingsCollection(): Fields
{
return $this->siblings;
}
/**
* Converts the field to a plain array
*/
public function toArray(): array
{
$array = parent::toArray();
unset($array['model']);
$array['hidden'] = $this->isHidden();
$array['saveable'] = $this->hasValue();
ksort($array);
return array_filter(
$array,
fn ($item) => $item !== null && is_object($item) === false
);
}
/**
* Returns the value of the field in a format to be stored by our storage classes
*/
public function toStoredValue(): mixed
{
$value = $this->toFormValue();
$store = $this->options['save'] ?? true;
if ($store === false) {
return null;
}
if ($store instanceof Closure) {
return $store->call($this, $value);
}
return $value;
}
/**
* Defines all validation rules
*/
protected function validations(): array
{
return $this->options['validations'] ?? [];
}
}

View file

@ -0,0 +1,364 @@
<?php
namespace Kirby\Form\Field;
use Kirby\Cms\App;
use Kirby\Cms\Block;
use Kirby\Cms\Blocks as BlocksCollection;
use Kirby\Cms\Fieldset;
use Kirby\Cms\Fieldsets;
use Kirby\Cms\ModelWithContent;
use Kirby\Data\Json;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Form\FieldClass;
use Kirby\Form\Form;
use Kirby\Form\Mixin\EmptyState;
use Kirby\Form\Mixin\Max;
use Kirby\Form\Mixin\Min;
use Kirby\Toolkit\Str;
use Throwable;
class BlocksField extends FieldClass
{
use EmptyState;
use Max;
use Min;
protected Fieldsets $fieldsets;
protected array $forms;
protected string|null $group;
protected bool $pretty;
protected mixed $value = [];
public function __construct(array $params = [])
{
$this->setFieldsets(
$params['fieldsets'] ?? null,
$params['model'] ?? App::instance()->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(
array $blocks,
string $to = 'toFormValues'
): array {
$result = [];
foreach ($blocks as $block) {
try {
$form = $this->fieldsetForm($block['type']);
// overwrite the block content with form values
$block['content'] = $form->reset()->fill(input: $block['content'])->$to();
// create id if not exists
$block['id'] ??= Str::uuid();
} catch (Throwable) {
// skip invalid blocks
} finally {
$result[] = $block;
}
}
return $result;
}
public function fields(string $type): array
{
return $this->fieldset($type)->fields();
}
public function fieldset(string $type): Fieldset
{
if ($fieldset = $this->fieldsets->find($type)) {
return $fieldset;
}
throw new NotFoundException(
'The fieldset ' . $type . ' could not be found'
);
}
protected function fieldsetForm(string $type): Form
{
return $this->forms[$type] ??= $this->form($this->fields($type));
}
public function fieldsets(): Fieldsets
{
return $this->fieldsets;
}
public function fieldsetGroups(): array|null
{
$groups = $this->fieldsets()->groups();
return $groups === [] ? null : $groups;
}
/**
* @psalm-suppress MethodSignatureMismatch
* @todo Remove psalm suppress after https://github.com/vimeo/psalm/issues/8673 is fixed
*/
public function fill(mixed $value): static
{
$value = BlocksCollection::parse($value);
$blocks = BlocksCollection::factory($value)->toArray();
$this->value = $this->blocksToValues($blocks);
return $this;
}
public function form(array $fields): Form
{
return new Form(
fields: $fields,
model: $this->model,
language: 'current'
);
}
public function isEmpty(): bool
{
return count($this->value()) === 0;
}
public function group(): string
{
return $this->group;
}
public function pretty(): bool
{
return $this->pretty;
}
/**
* Paste action for blocks:
* - generates new uuids for the blocks
* - filters only supported fieldsets
* - applies max limit if defined
*/
public function pasteBlocks(array $blocks): array
{
$blocks = $this->blocksToValues($blocks);
foreach ($blocks as $index => &$block) {
$block['id'] = Str::uuid();
// remove the block if it's not available
try {
$this->fieldset($block['type']);
} catch (Throwable) {
unset($blocks[$index]);
}
}
return array_values($blocks);
}
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' => fn (): array => ['uuid' => Str::uuid()]
],
[
'pattern' => 'paste',
'method' => 'POST',
'action' => function () use ($field): array {
$request = App::instance()->request();
$value = BlocksCollection::parse($request->get('html'));
$blocks = BlocksCollection::factory($value);
return $field->pasteBlocks($blocks->toArray());
}
],
[
'pattern' => 'fieldsets/(:any)',
'method' => 'GET',
'action' => function (
string $fieldsetType
) use ($field): array {
$fields = $field->fields($fieldsetType);
$form = $field->form($fields);
$form->fill(input: $form->defaults());
return Block::factory([
'content' => $form->toFormValues(),
'type' => $fieldsetType
])->toArray();
}
],
[
'pattern' => 'fieldsets/(:any)/fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (
string $fieldsetType,
string $fieldName,
string|null $path = null
) use ($field) {
$fields = $field->fields($fieldsetType);
$field = $field->form($fields)->field($fieldName);
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => [
...$this->data(),
'field' => $field
]
]);
return $fieldApi->call(
$path,
$this->requestMethod(),
$this->requestData()
);
}
],
];
}
protected function setDefault(mixed $default = null): void
{
// set id for blocks if not exists
if (is_array($default) === true) {
array_walk($default, function (&$block) {
$block['id'] ??= Str::uuid();
});
}
parent::setDefault($default);
}
protected function setFieldsets(
string|array|null $fieldsets,
ModelWithContent $model
): void {
if (is_string($fieldsets) === true) {
$fieldsets = [];
}
$this->fieldsets = Fieldsets::factory(
$fieldsets,
['parent' => $model]
);
}
protected function setGroup(string|null $group = null): void
{
$this->group = $group;
}
protected function setPretty(bool $pretty = false): void
{
$this->pretty = $pretty;
}
public function toStoredValue(bool $default = false): mixed
{
$value = $this->toFormValue($default);
$blocks = $this->blocksToValues((array)$value, 'toStoredValues');
// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if ($blocks === []) {
return '';
}
return Json::encode($blocks, pretty: $this->pretty());
}
public function validations(): array
{
return [
'blocks' => function ($value) {
if ($this->min && count($value) < $this->min) {
throw new InvalidArgumentException(
key: match ($this->min) {
1 => 'blocks.min.singular',
default => 'blocks.min.plural'
},
data: ['min' => $this->min]
);
}
if ($this->max && count($value) > $this->max) {
throw new InvalidArgumentException(
key: match ($this->max) {
1 => 'blocks.max.singular',
default => 'blocks.max.plural'
},
data: ['max' => $this->max]
);
}
$forms = [];
$index = 0;
foreach ($value as $block) {
$index++;
$type = $block['type'];
// create the form for the block
// and cache it for later use
if (isset($forms[$type]) === false) {
try {
$fieldset = $this->fieldset($type);
$fields = $fieldset->fields() ?? [];
$forms[$type] = $this->form($fields);
} catch (Throwable) {
// skip invalid blocks
continue;
}
}
// overwrite the content with the serialized form
$form = $forms[$type]->reset()->fill($block['content']);
foreach ($form->fields() as $field) {
$errors = $field->errors();
// rough first validation
if (count($errors) > 0) {
throw new InvalidArgumentException(
key:'blocks.validation',
data: [
'field' => $field->label(),
'fieldset' => $fieldset->name(),
'index' => $index
]
);
}
}
}
return true;
}
];
}
}

View file

@ -0,0 +1,212 @@
<?php
namespace Kirby\Form\Field;
use Kirby\Data\Data;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Form\FieldClass;
use Kirby\Form\Form;
use Kirby\Form\Mixin\EmptyState;
use Kirby\Form\Mixin\Max;
use Kirby\Form\Mixin\Min;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
/**
* Main class file of the entries field
*
* @package Kirby Field
* @author Ahmet Bora <ahmet@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
*/
class EntriesField extends FieldClass
{
use EmptyState;
use Max;
use Min;
protected array $field;
protected Form $form;
protected bool $sortable = true;
protected mixed $value = [];
public function __construct(array $params = [])
{
parent::__construct($params);
$this->setEmpty($params['empty'] ?? null);
$this->setField($params['field'] ?? null);
$this->setMax($params['max'] ?? null);
$this->setMin($params['min'] ?? null);
$this->setSortable($params['sortable'] ?? true);
}
public function field(): array
{
return $this->field;
}
public function fieldProps(): array
{
return $this->form()->fields()->first()->toArray();
}
/**
* @psalm-suppress MethodSignatureMismatch
* @todo Remove psalm suppress after https://github.com/vimeo/psalm/issues/8673 is fixed
*/
public function fill(mixed $value): static
{
$this->value = Data::decode($value ?? '', 'yaml');
return $this;
}
public function form(): Form
{
return $this->form ??= new Form(
fields: [$this->field()],
model: $this->model
);
}
public function props(): array
{
return [
...parent::props(),
'empty' => $this->empty(),
'field' => $this->fieldProps(),
'max' => $this->max(),
'min' => $this->min(),
'sortable' => $this->sortable(),
];
}
protected function setField(array|string|null $attrs = null): void
{
if (is_string($attrs) === true) {
$attrs = ['type' => $attrs];
}
$attrs ??= ['type' => 'text'];
if (in_array($attrs['type'], $this->supports()) === false) {
throw new InvalidArgumentException(
key: 'entries.supports',
data: ['type' => $attrs['type']]
);
}
// remove the unsupported props from the entry field
unset($attrs['counter'], $attrs['label']);
$this->field = $attrs;
}
protected function setSortable(bool|null $sortable = true): void
{
$this->sortable = $sortable;
}
public function sortable(): bool
{
return $this->sortable;
}
public function supports(): array
{
return [
'color',
'date',
'email',
'number',
'select',
'slug',
'tel',
'text',
'time',
'url'
];
}
public function toFormValue(): mixed
{
$form = $this->form();
$value = parent::toFormValue() ?? $this->emptyValue();
return A::map(
$value,
fn ($value) => $form
->reset()
->fill(input: [$value])
->fields()
->first()
->toFormValue()
);
}
public function toStoredValue(): mixed
{
$form = $this->form();
$value = parent::toStoredValue();
return A::map(
$value,
fn ($value) => $form
->reset()
->submit(input: [$value])
->fields()
->first()
->toStoredValue()
);
}
public function validations(): array
{
return [
'entries' => function ($value) {
if ($this->min && count($value) < $this->min) {
throw new InvalidArgumentException(
key: match ($this->min) {
1 => 'entries.min.singular',
default => 'entries.min.plural'
},
data: ['min' => $this->min]
);
}
if ($this->max && count($value) > $this->max) {
throw new InvalidArgumentException(
key: match ($this->max) {
1 => 'entries.max.singular',
default => 'entries.max.plural'
},
data: ['max' => $this->max]
);
}
$form = $this->form();
foreach ($value as $index => $val) {
$form->reset()->submit(input: [$val]);
foreach ($form->fields() as $field) {
$errors = $field->errors();
if ($errors !== []) {
throw new InvalidArgumentException(
key: 'entries.validation',
data: [
'field' => $this->label() ?? Str::ucfirst($this->name()),
'index' => $index + 1
]
);
}
}
}
}
];
}
}

View file

@ -0,0 +1,371 @@
<?php
namespace Kirby\Form\Field;
use Kirby\Cms\App;
use Kirby\Cms\Blueprint;
use Kirby\Cms\Fieldset;
use Kirby\Cms\Layout;
use Kirby\Cms\Layouts;
use Kirby\Data\Data;
use Kirby\Data\Json;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Form\Form;
use Kirby\Toolkit\Str;
use Throwable;
class LayoutField extends BlocksField
{
protected array|null $layouts;
protected array|null $selector;
protected Fieldset|null $settings;
public function __construct(array $params)
{
$this->setModel($params['model'] ?? App::instance()->site());
$this->setLayouts($params['layouts'] ?? ['1/1']);
$this->setSelector($params['selector'] ?? null);
$this->setSettings($params['settings'] ?? null);
parent::__construct($params);
}
/**
* @psalm-suppress MethodSignatureMismatch
* @todo Remove psalm suppress after https://github.com/vimeo/psalm/issues/8673 is fixed
*/
public function fill(mixed $value): static
{
$attrs = $this->attrsForm();
$value = Data::decode($value, type: 'json', fail: false);
$layouts = Layouts::factory($value, ['parent' => $this->model])->toArray();
foreach ($layouts as $layoutIndex => $layout) {
if ($this->settings !== null) {
$layouts[$layoutIndex]['attrs'] = $attrs->reset()->fill($layout['attrs'])->toFormValues();
}
foreach ($layout['columns'] as $columnIndex => $column) {
$layouts[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks']);
}
}
$this->value = $layouts;
return $this;
}
public function attrsForm(): Form
{
return new Form(
fields: $this->settings()?->fields() ?? [],
model: $this->model
);
}
public function layouts(): array|null
{
return $this->layouts;
}
/**
* Creates form values for each layout
*/
public function layoutsToValues(array $layouts): array
{
foreach ($layouts as &$layout) {
$layout['id'] ??= Str::uuid();
$layout['columns'] ??= [];
array_walk($layout['columns'], function (&$column) {
$column['id'] ??= Str::uuid();
$column['blocks'] = $this->blocksToValues($column['blocks'] ?? []);
});
}
return $layouts;
}
/**
* Paste action for layouts:
* - generates new uuids for layout, column and blocks
* - filters only supported layouts
* - filters only supported fieldsets
*/
public function pasteLayouts(array $layouts): array
{
$layouts = $this->layoutsToValues($layouts);
foreach ($layouts as $layoutIndex => &$layout) {
$layout['id'] = Str::uuid();
// remove the row if layout not available for the pasted layout field
$columns = array_column($layout['columns'], 'width');
if (in_array($columns, $this->layouts(), true) === false) {
unset($layouts[$layoutIndex]);
continue;
}
array_walk($layout['columns'], function (&$column) {
$column['id'] = Str::uuid();
array_walk($column['blocks'], function (&$block, $index) use ($column) {
$block['id'] = Str::uuid();
// remove the block if it's not available
try {
$this->fieldset($block['type']);
} catch (Throwable) {
unset($column['blocks'][$index]);
}
});
});
}
return $layouts;
}
public function props(): array
{
return [
...parent::props(),
'layouts' => $this->layouts(),
'selector' => $this->selector(),
'settings' => $this->settings()?->toArray()
];
}
public function routes(): array
{
$field = $this;
$routes = parent::routes();
$routes[] = [
'pattern' => 'layout',
'method' => 'POST',
'action' => function () use ($field): array {
$request = App::instance()->request();
$columns = $request->get('columns') ?? ['1/1'];
$form = $field->attrsForm();
$form->fill(input: $form->defaults());
$form->submit(input: $request->get('attrs') ?? []);
return Layout::factory([
'attrs' => $form->toFormValues(),
'columns' => array_map(fn ($width) => [
'blocks' => [],
'id' => Str::uuid(),
'width' => $width,
], $columns)
])->toArray();
},
];
$routes[] = [
'pattern' => 'layout/paste',
'method' => 'POST',
'action' => function () use ($field): array {
$request = App::instance()->request();
$value = Layouts::parse($request->get('json'));
$layouts = Layouts::factory($value);
return $field->pasteLayouts($layouts->toArray());
}
];
$routes[] = [
'pattern' => 'fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (
string $fieldName,
string|null $path = null
) use ($field): array {
$form = $field->attrsForm();
$field = $form->field($fieldName);
$fieldApi = $this->clone([
'routes' => $field->api(),
'data' => [
...$this->data(),
'field' => $field
]
]);
return $fieldApi->call(
$path,
$this->requestMethod(),
$this->requestData()
);
}
];
return $routes;
}
public function selector(): array|null
{
return $this->selector;
}
protected function setDefault(mixed $default = null): void
{
// set id for layouts, columns and blocks within layout if not exists
if (is_array($default) === true) {
array_walk($default, function (&$layout) {
$layout['id'] ??= Str::uuid();
// set columns id within layout
if (isset($layout['columns']) === true) {
array_walk($layout['columns'], function (&$column) {
$column['id'] ??= Str::uuid();
// set blocks id within column
if (isset($column['blocks']) === true) {
array_walk($column['blocks'], function (&$block) {
$block['id'] ??= Str::uuid();
});
}
});
}
});
}
parent::setDefault($default);
}
protected function setLayouts(array $layouts = []): void
{
$this->layouts = array_map(
fn ($layout) => Str::split($layout),
$layouts
);
}
/**
* Layout selector's styles such as size (`small`, `medium`, `large` or `huge`) and columns
*/
protected function setSelector(array|null $selector = null): void
{
$this->selector = $selector;
}
protected function setSettings(array|string|null $settings = null): void
{
if (empty($settings) === true) {
$this->settings = null;
return;
}
$settings = Blueprint::extend($settings);
$settings['icon'] = 'dashboard';
$settings['type'] = 'layout';
$settings['parent'] = $this->model();
$this->settings = Fieldset::factory($settings);
}
public function settings(): Fieldset|null
{
return $this->settings;
}
public function toStoredValue(bool $default = false): mixed
{
$attrs = $this->attrsForm();
$value = $this->toFormValue($default);
$value = Layouts::factory($value, ['parent' => $this->model])->toArray();
// returns empty string to avoid storing empty array as string `[]`
// and to consistency work with `$field->isEmpty()`
if ($value === []) {
return '';
}
foreach ($value as $layoutIndex => $layout) {
if ($this->settings !== null) {
$value[$layoutIndex]['attrs'] = $attrs->reset()->fill($layout['attrs'])->toStoredValues();
}
foreach ($layout['columns'] as $columnIndex => $column) {
$value[$layoutIndex]['columns'][$columnIndex]['blocks'] = $this->blocksToValues($column['blocks'] ?? [], 'content');
}
}
return Json::encode($value, pretty: $this->pretty());
}
public function validations(): array
{
return [
'layout' => function ($value) {
$attrsForm = $this->attrsForm();
$blockForms = [];
$layoutIndex = 0;
foreach ($value as $layout) {
$layoutIndex++;
// validate settings form
$form = $attrsForm->reset()->fill($layout['attrs'] ?? []);
foreach ($form->fields() as $field) {
$errors = $field->errors();
if (count($errors) > 0) {
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'];
if (isset($blockForms[$blockType]) === false) {
try {
$fieldset = $this->fieldset($blockType);
$fields = $this->fields($blockType) ?? [];
$blockForms[$blockType] = $this->form($fields);
} catch (Throwable) {
// skip invalid blocks
continue;
}
}
// overwrite the content with the serialized form
$form = $blockForms[$blockType]->reset()->fill($block['content']);
foreach ($form->fields() as $field) {
$errors = $field->errors();
// rough first validation
if (count($errors) > 0) {
throw new InvalidArgumentException(
key: 'layout.validation.block',
data: [
'blockIndex' => $blockIndex,
'field' => $field->label(),
'fieldset' => $fieldset->name(),
'layoutIndex' => $layoutIndex
]
);
}
}
}
}
}
return true;
}
];
}
}

View file

@ -0,0 +1,74 @@
<?php
namespace Kirby\Form\Field;
use Kirby\Form\FieldClass;
use Kirby\Panel\Ui\Stats;
/**
* Stats field
*
* @package Kirby Field
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.1.0
*/
class StatsField extends FieldClass
{
/**
* Array or query string for reports. Each report needs a `label` and `value` and can have additional `info`, `link`, `icon` and `theme` settings.
*/
protected array|string $reports;
/**
* The size of the report cards. Available sizes: `tiny`, `small`, `medium`, `large`
*/
protected string $size;
/**
* Cache for the Stats UI component
*/
protected Stats $stats;
public function __construct(array $params)
{
parent::__construct($params);
$this->reports = $params['reports'] ?? [];
$this->size = $params['size'] ?? 'large';
}
public function hasValue(): bool
{
return false;
}
public function reports(): array
{
return $this->stats()->reports();
}
public function size(): string
{
return $this->stats()->size();
}
public function stats(): Stats
{
return $this->stats ??= Stats::from(
model: $this->model,
reports: $this->reports,
size: $this->size
);
}
public function props(): array
{
return [
...parent::props(),
...$this->stats()->props()
];
}
}

View file

@ -0,0 +1,225 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\HasSiblings;
use Kirby\Toolkit\I18n;
/**
* Abstract field class to be used instead
* of functional field components for more
* control.
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*
* @use \Kirby\Cms\HasSiblings<\Kirby\Form\Fields>
*/
abstract class FieldClass
{
use HasSiblings;
use Mixin\After;
use Mixin\Api;
use Mixin\Autofocus;
use Mixin\Before;
use Mixin\Help;
use Mixin\Icon;
use Mixin\Label;
use Mixin\Model;
use Mixin\Placeholder;
use Mixin\Translatable;
use Mixin\Validation;
use Mixin\Value;
use Mixin\When;
use Mixin\Width;
protected bool $disabled;
protected string|null $name;
protected Fields $siblings;
public function __construct(
protected array $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'] ?? null);
$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 __call(string $param, array $args): mixed
{
if (isset($this->$param) === true) {
return $this->$param;
}
return $this->params[$param] ?? null;
}
/**
* Returns optional dialog routes for the field
*/
public function dialogs(): array
{
return [];
}
/**
* If `true`, the field is no longer editable and will not be saved
*/
public function disabled(): bool
{
return $this->disabled;
}
/**
* Returns optional drawer routes for the field
*/
public function drawers(): array
{
return [];
}
protected function i18n(string|array|null $param = null): string|null
{
return empty($param) === false ? I18n::translate($param, $param) : null;
}
public function id(): string
{
return $this->name();
}
public function isDisabled(): bool
{
return $this->disabled;
}
public function isHidden(): bool
{
return false;
}
/**
* Returns the field name
*/
public function name(): string
{
return $this->name ?? $this->type();
}
/**
* Returns all original params for the field
*/
public function params(): array
{
return $this->params;
}
/**
* Define the props that will be sent to
* the Vue component
*/
public function props(): array
{
return [
'after' => $this->after(),
'autofocus' => $this->autofocus(),
'before' => $this->before(),
'default' => $this->default(),
'disabled' => $this->isDisabled(),
'help' => $this->help(),
'hidden' => $this->isHidden(),
'icon' => $this->icon(),
'label' => $this->label(),
'name' => $this->name(),
'placeholder' => $this->placeholder(),
'required' => $this->isRequired(),
'saveable' => $this->hasValue(),
'translate' => $this->translate(),
'type' => $this->type(),
'when' => $this->when(),
'width' => $this->width(),
];
}
/**
* @since 5.2.0
* @todo Move to `Value` mixin once array-based fields are unsupported
*/
public function reset(): static
{
$this->value = $this->emptyValue();
return $this;
}
protected function setDisabled(bool $disabled = false): void
{
$this->disabled = $disabled;
}
protected function setName(string|null $name = null): void
{
$this->name = strtolower($name ?? $this->type());
}
protected function setSiblings(Fields|null $siblings = null): void
{
$this->siblings = $siblings ?? new Fields([$this]);
}
protected function siblingsCollection(): Fields
{
return $this->siblings;
}
/**
* Parses a string template in the given value
*/
protected function stringTemplate(string|null $string = null): string|null
{
if ($string !== null) {
return $this->model->toString($string);
}
return null;
}
/**
* Converts the field to a plain array
*/
public function toArray(): array
{
$props = $this->props();
ksort($props);
return array_filter($props, fn ($item) => $item !== null);
}
/**
* Returns the field type
*/
public function type(): string
{
return lcfirst(basename(str_replace(['\\', 'Field'], ['/', ''], static::class)));
}
}

436
kirby/src/Form/Fields.php Normal file
View file

@ -0,0 +1,436 @@
<?php
namespace Kirby\Form;
use Closure;
use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Cms\ModelWithContent;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Str;
/**
* A collection of Field objects
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @extends \Kirby\Toolkit\Collection<\Kirby\Form\Field|\Kirby\Form\FieldClass>
*/
class Fields extends Collection
{
protected Language $language;
protected ModelWithContent $model;
protected array $passthrough = [];
public function __construct(
array $fields = [],
ModelWithContent|null $model = null,
Language|string|null $language = null
) {
$this->model = $model ?? App::instance()->site();
$this->language = Language::ensure($language ?? 'current');
foreach ($fields as $name => $field) {
$this->__set($name, $field);
}
}
/**
* Internal setter for each object in the Collection.
* This takes care of validation and of setting
* the collection prop on each object correctly.
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass|array $field
*/
public function __set(string $name, $field): void
{
if (is_array($field) === true) {
// use the array key as name if the name is not set
$field['model'] ??= $this->model;
$field['name'] ??= $name;
$field = Field::factory($field['type'], $field, $this);
}
parent::__set($field->name(), $field);
}
/**
* Returns an array with the default value of each field
*
* @since 5.0.0
*/
public function defaults(): array
{
return $this->toArray(fn ($field) => $field->default());
}
/**
* An array of all found in all fields errors
*/
public function errors(): array
{
$errors = [];
foreach ($this->data as $name => $field) {
$fieldErrors = $field->errors();
if ($fieldErrors !== []) {
$errors[$name] = [
'label' => $field->label(),
'message' => $fieldErrors
];
}
}
return $errors;
}
/**
* Get the field object by name
* and handle nested fields correctly
*
* @since 5.0.0
* @throws \Kirby\Exception\NotFoundException
*/
public function field(string $name): Field|FieldClass
{
if ($field = $this->find($name)) {
return $field;
}
throw new NotFoundException(
message: 'The field could not be found'
);
}
/**
* Sets the value for each field with a matching key in the input array
*
* @since 5.0.0
*/
public function fill(
array $input,
bool $passthrough = true
): static {
if ($passthrough === true) {
$this->passthrough($input);
}
foreach ($input as $name => $value) {
if (!$field = $this->get($name)) {
continue;
}
// don't change the value of non-value field
if ($field->hasValue() === false) {
continue;
}
// resolve closure values
if ($value instanceof Closure) {
$value = $value($field->toFormValue());
}
$field->fill($value);
}
return $this;
}
/**
* Find a field by key/name
*/
public function findByKey(string $key): Field|FieldClass|null
{
if (str_contains($key, '+')) {
return $this->findByKeyRecursive($key);
}
return parent::findByKey($key);
}
/**
* Find fields in nested forms recursively
*/
public function findByKeyRecursive(string $key): Field|FieldClass|null
{
$fields = $this;
$names = Str::split($key, '+');
$index = 0;
$count = count($names);
$field = null;
foreach ($names as $name) {
$index++;
// search for the field by name
$field = $fields->get($name);
// if the field cannot be found,
// there's no point in going further
if ($field === null) {
return null;
}
// there are more parts in the key
if ($index < $count) {
$form = $field->form();
// the search can only continue for
// fields with valid nested forms
if ($form instanceof Form === false) {
return null;
}
$fields = $form->fields();
}
}
return $field;
}
/**
* Creates a new Fields instance for the given model and language
*
* @since 5.0.0
*/
public static function for(
ModelWithContent $model,
Language|string|null $language = null
): static {
return new static(
fields: $model->blueprint()->fields(),
model: $model,
language: $language,
);
}
/**
* Returns the language of the fields
*
* @since 5.0.0
*/
public function language(): Language
{
return $this->language;
}
/**
* Adds values to the passthrough array
* which will be added to the form data
* if the field does not exist
*
* @since 5.0.0
*/
public function passthrough(array|null $values = null): static|array
{
// use passthrough method as getter if the value is null
if ($values === null) {
return $this->passthrough;
}
foreach ($values as $key => $value) {
$key = strtolower($key);
// check if the field exists and don't passthrough
// values for existing fields
if ($this->get($key) !== null) {
continue;
}
// resolve closure values
if ($value instanceof Closure) {
$value = $value($this->passthrough[$key] ?? null);
}
$this->passthrough[$key] = $value;
}
return $this;
}
/**
* Resets the value of each field
*
* @since 5.0.0
*/
public function reset(): static
{
// reset the passthrough values
$this->passthrough = [];
// reset the values of each field
foreach ($this->data as $field) {
if ($field instanceof Field) {
$field->fillWithEmptyValue();
} else {
$field->reset();
}
}
return $this;
}
/**
* Sets the value for each field with a matching key in the input array
* but only if the field is not disabled
*
* @since 5.0.0
* @param bool $passthrough If true, values for undefined fields will be submitted
* @param bool $force If true, values for fields that cannot be submitted (e.g. disabled or untranslatable fields) will be submitted
*/
public function submit(
array $input,
bool $passthrough = true,
bool $force = false
): static {
$language = $this->language();
if ($passthrough === true) {
$this->passthrough($input);
}
foreach ($input as $name => $value) {
if (!$field = $this->get($name)) {
continue;
}
// don't submit fields without a value
if ($force === true && $field->hasValue() === false) {
continue;
}
// don't submit fields that are not submittable
if ($force === false && $field->isSubmittable($language) === false) {
continue;
}
// resolve closure values
if ($value instanceof Closure) {
$value = $value($field->toFormValue());
}
// submit the value to the field
// the field class might override this method
// to handle submitted values differently
$field->submit($value);
}
// reset the errors cache
return $this;
}
/**
* Converts the fields collection to an
* array and also does that for every
* included field.
*/
public function toArray(Closure|null $map = null): array
{
return A::map($this->data, $map ?? fn ($field) => $field->toArray());
}
/**
* Returns an array with the form value of each field
* (e.g. used as data for Panel Vue components)
*
* @since 5.0.0
*/
public function toFormValues(): array
{
return $this->toValues(
fn ($field) => $field->toFormValue(),
fn ($field) => $field->hasValue()
);
}
/**
* Returns an array with the props of each field
* for the frontend
*
* @since 5.0.0
*/
public function toProps(): array
{
$fields = $this->data;
$props = [];
$language = $this->language();
$permissions = $this->model->permissions()->can('update');
foreach ($fields as $name => $field) {
$props[$name] = $field->toArray();
// the field should be disabled in the form if the user
// has no update permissions for the model or if the field
// is not translatable into the current language
if ($permissions === false || $field->isTranslatable($language) === false) {
$props[$name]['disabled'] = true;
}
// the value should not be included in the props
// we pass on the values to the frontend via the model
// view props to make them globally available for the view.
unset($props[$name]['value']);
}
return $props;
}
/**
* Returns an array with the stored value of each field
* (e.g. used for saving to content storage)
*
* @since 5.0.0
*/
public function toStoredValues(): array
{
return $this->toValues(
fn ($field) => $field->toStoredValue(),
fn ($field) => $field->isStorable($this->language())
);
}
/**
* Returns an array with the values of each field
* and adds passthrough values if they don't exist
* @unstable
*/
protected function toValues(Closure $method, Closure $filter): array
{
$values = $this->filter($filter)->toArray($method);
foreach ($this->passthrough as $key => $value) {
if (isset($values[$key]) === false) {
$values[$key] = $value;
}
}
return $values;
}
/**
* Checks for errors in all fields and throws an
* exception if there are any
*
* @since 5.0.0
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function validate(): void
{
$errors = $this->errors();
if ($errors !== []) {
throw new InvalidArgumentException(
fallback: 'Invalid form with errors',
details: $errors
);
}
}
}

400
kirby/src/Form/Form.php Normal file
View file

@ -0,0 +1,400 @@
<?php
namespace Kirby\Form;
use Kirby\Cms\File;
use Kirby\Cms\Language;
use Kirby\Cms\ModelWithContent;
use Kirby\Data\Data;
use Kirby\Toolkit\A;
/**
* The main form class, that is being
* used to create a list of form fields
* and handles global form validation
* and submission
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Form
{
/**
* Fields in the form
*/
protected Fields $fields;
/**
* Form constructor
*/
public function __construct(
array $props = [],
array $fields = [],
ModelWithContent|null $model = null,
Language|string|null $language = null
) {
if ($props !== []) {
$this->legacyConstruct(...$props);
return;
}
$this->fields = new Fields(
fields: $fields,
model: $model,
language: $language
);
}
/**
* Returns the data required to write to the content file
* Doesn't include default and null values
*
* @deprecated 5.0.0 Use `::toStoredValues()` instead
*/
public function content(): array
{
return $this->data(false, false);
}
/**
* Returns data for all fields in the form
*
* @deprecated 5.0.0 Use `::toStoredValues()` instead
*/
public function data($defaults = false, bool $includeNulls = true): array
{
$data = [];
$language = $this->fields->language();
foreach ($this->fields as $field) {
if ($field->isStorable($language) === false) {
if ($includeNulls === true) {
$data[$field->name()] = null;
}
continue;
}
if ($defaults === true && $field->isEmpty() === true) {
$field->fill($field->default());
}
$data[$field->name()] = $field->toStoredValue();
}
foreach ($this->fields->passthrough() as $key => $value) {
if (isset($data[$key]) === false) {
$data[$key] = $value;
}
}
return $data;
}
/**
* Returns an array with the default value of each field
*
* @since 5.0.0
*/
public function defaults(): array
{
return $this->fields->defaults();
}
/**
* An array of all found errors
*/
public function errors(): array
{
return $this->fields->errors();
}
/**
* Get the field object by name
* and handle nested fields correctly
*
* @throws \Kirby\Exception\NotFoundException
*/
public function field(string $name): Field|FieldClass
{
return $this->fields->field($name);
}
/**
* Returns form fields
*/
public function fields(): Fields
{
return $this->fields;
}
/**
* Sets the value for each field with a matching key in the input array
*
* @since 5.0.0
*/
public function fill(
array $input,
bool $passthrough = true
): static {
$this->fields->fill(
input: $input,
passthrough: $passthrough
);
return $this;
}
/**
* Creates a new Form instance for the given model with the fields
* from the blueprint and the values from the content
*/
public static function for(
ModelWithContent $model,
array $props = [],
Language|string|null $language = null,
): static {
if ($props !== []) {
return static::legacyFor(
$model,
...$props
);
}
$form = new static(
fields: $model->blueprint()->fields(),
model: $model,
language: $language
);
// fill the form with the latest content of the model
$form->fill(input: $model->content($form->language())->toArray());
return $form;
}
/**
* Checks if the form is invalid
*/
public function isInvalid(): bool
{
return $this->isValid() === false;
}
/**
* Checks if the form is valid
*/
public function isValid(): bool
{
return $this->fields->errors() === [];
}
/**
* Returns the language of the form
*
* @since 5.0.0
*/
public function language(): Language
{
return $this->fields->language();
}
/**
* Legacy constructor to support the old props array
*
* @deprecated 5.0.0 Use the new constructor with named parameters instead
*/
protected function legacyConstruct(
array $fields = [],
ModelWithContent|null $model = null,
Language|string|null $language = null,
array $values = [],
array $input = [],
bool $strict = false
): void {
$this->__construct(
fields: $fields,
model: $model,
language: $language
);
$this->fill(
input: $values,
passthrough: $strict === false
);
$this->submit(
input: $input,
passthrough: $strict === false
);
}
/**
* Legacy for method to support the old props array
*
* @deprecated 5.0.0 Use `::for()` with named parameters instead
*/
protected static function legacyFor(
ModelWithContent $model,
Language|string|null $language = null,
bool $strict = false,
array|null $input = [],
array|null $values = [],
bool $ignoreDisabled = false
): static {
$form = static::for(
model: $model,
language: $language,
);
$form->fill(
input: $values ?? [],
passthrough: $strict === false
);
$form->submit(
input: $input ?? [],
passthrough: $strict === false
);
return $form;
}
/**
* Adds values to the passthrough array
* which will be added to the form data
* if the field does not exist
*
* @since 5.0.0
*/
public function passthrough(
array|null $values = null
): static|array {
if ($values === null) {
return $this->fields->passthrough();
}
$this->fields->passthrough(
values: $values
);
return $this;
}
/**
* Resets the value of each field
*
* @since 5.0.0
*/
public function reset(): static
{
$this->fields->reset();
return $this;
}
/**
* Converts the data of fields to strings
*
* @deprecated 5.0.0 Use `::toStoredValues()` instead
*/
public function strings($defaults = false): array
{
return A::map(
$this->data($defaults),
fn ($value) => match (true) {
is_array($value) => Data::encode($value, 'yaml'),
default => $value
}
);
}
/**
* Sets the value for each field with a matching key in the input array
* but only if the field is not disabled
*
* @since 5.0.0
* @param bool $passthrough If true, values for undefined fields will be submitted
* @param bool $force If true, values for fields that cannot be submitted (e.g. disabled or untranslatable fields) will be submitted
*/
public function submit(
array $input,
bool $passthrough = true,
bool $force = false
): static {
$this->fields->submit(
input: $input,
passthrough: $passthrough,
force: $force
);
return $this;
}
/**
* Converts the form to a plain array
*/
public function toArray(): array
{
$array = [
'errors' => $this->fields->errors(),
'fields' => $this->fields->toArray(),
'invalid' => $this->isInvalid()
];
return $array;
}
/**
* Returns an array with the form value of each field
* (e.g. used as data for Panel Vue components)
*
* @since 5.0.0
*/
public function toFormValues(): array
{
return $this->fields->toFormValues();
}
/**
* Returns an array with the props of each field
* for the frontend
*
* @since 5.0.0
*/
public function toProps(): array
{
return $this->fields->toProps();
}
/**
* Returns an array with the stored value of each field
* (e.g. used for saving to content storage)
*
* @since 5.0.0
*/
public function toStoredValues(): array
{
return $this->fields->toStoredValues();
}
/**
* Validates the form and throws an exception if there are any errors
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public function validate(): void
{
$this->fields->validate();
}
/**
* Returns form values
*
* @deprecated 5.0.0 Use `::toFormValues()` instead
*/
public function values(): array
{
return $this->fields->toFormValues();
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Kirby\Form\Mixin;
trait After
{
/**
* Optional text that will be shown after the input
*/
protected string|null $after;
public function after(): string|null
{
return $this->stringTemplate($this->after);
}
protected function setAfter(array|string|null $after = null): void
{
$this->after = $this->i18n($after);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace Kirby\Form\Mixin;
trait Api
{
public function api(): array
{
return $this->routes();
}
/**
* Routes for the field API
*/
public function routes(): array
{
return [];
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Kirby\Form\Mixin;
trait Autofocus
{
/**
* Sets the focus on this field when the form loads. Only the first field with this label gets
*/
protected bool $autofocus;
public function autofocus(): bool
{
return $this->autofocus;
}
protected function setAutofocus(bool $autofocus = false): void
{
$this->autofocus = $autofocus;
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Kirby\Form\Mixin;
trait Before
{
/**
* Optional text that will be shown before the input
*/
protected string|null $before;
public function before(): string|null
{
return $this->stringTemplate($this->before);
}
protected function setBefore(array|string|null $before = null): void
{
$this->before = $this->i18n($before);
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Kirby\Form\Mixin;
trait EmptyState
{
/**
* Sets the text for the empty state box
*/
protected string|null $empty;
protected function setEmpty(string|array|null $empty = null): void
{
$this->empty = $this->i18n($empty);
}
public function empty(): string|null
{
return $this->stringTemplate($this->empty);
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace Kirby\Form\Mixin;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Help
{
/**
* Optional help text below the field
*/
protected string|null $help;
public function help(): string|null
{
if (empty($this->help) === false) {
$help = $this->stringTemplate($this->help);
$help = $this->kirby()->kirbytext($help);
return $help;
}
return null;
}
protected function setHelp(array|string|null $help = null): void
{
$this->help = $this->i18n($help);
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace Kirby\Form\Mixin;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Icon
{
/**
* Optional icon that will be shown at the end of the field
*/
protected string|null $icon;
public function icon(): string|null
{
return $this->icon;
}
protected function setIcon(string|null $icon = null): void
{
$this->icon = $icon;
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Kirby\Form\Mixin;
use Kirby\Toolkit\Str;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Label
{
/**
* The field label can be set as string or associative array with translations
*/
protected string|null $label;
public function label(): string|null
{
return $this->stringTemplate(
$this->label ?? Str::ucfirst($this->name())
);
}
protected function setLabel(array|string|null $label = null): void
{
$this->label = $this->i18n($label);
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Kirby\Form\Mixin;
trait Max
{
/**
* Sets the maximum number of allowed items in the field
*/
protected int|null $max;
public function max(): int|null
{
return $this->max;
}
protected function setMax(int|null $max = null)
{
$this->max = $max;
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Kirby\Form\Mixin;
trait Min
{
/**
* Sets the minimum number of required items in the field
*/
protected int|null $min;
public function min(): int|null
{
// set min to at least 1, if required
if ($this->required === true) {
return $this->min ?? 1;
}
return $this->min;
}
protected function setMin(int|null $min = null)
{
$this->min = $min;
}
public function isRequired(): bool
{
// set required to true if min is set
if ($this->min) {
return true;
}
return $this->required;
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace Kirby\Form\Mixin;
use Kirby\Cms\App;
use Kirby\Cms\ModelWithContent;
trait Model
{
protected ModelWithContent $model;
/**
* Returns the Kirby instance
*/
public function kirby(): App
{
return $this->model->kirby();
}
/**
* Returns the parent model
*/
public function model(): ModelWithContent
{
return $this->model;
}
/**
* Sets the parent model
*/
protected function setModel(ModelWithContent|null $model = null): void
{
$this->model = $model ?? App::instance()->site();
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Kirby\Form\Mixin;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Placeholder
{
/**
* Optional placeholder value that will be shown when the field is empty
*/
protected array|string|null $placeholder;
public function placeholder(): string|null
{
return $this->stringTemplate(
$this->placeholder
);
}
protected function setPlaceholder(array|string|null $placeholder = null): void
{
$this->placeholder = $this->i18n($placeholder);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Kirby\Form\Mixin;
use Kirby\Cms\Language;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Translatable
{
/**
* Should the field be translatable?
*/
protected bool $translate = true;
/**
* Should the field be translatable into the given language?
*
* @since 5.0.0
*/
public function isTranslatable(Language $language): bool
{
if ($this->translate() === false && $language->isDefault() === false) {
return false;
}
return true;
}
protected function setTranslate(bool $translate = true): void
{
$this->translate = $translate;
}
public function translate(): bool
{
return $this->translate;
}
}

View file

@ -0,0 +1,117 @@
<?php
namespace Kirby\Form\Mixin;
use Closure;
use Exception;
use Kirby\Form\Validations;
use Kirby\Toolkit\A;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\V;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Validation
{
/**
* If `true`, the field has to be filled in correctly to be saved.
*/
protected bool $required;
/**
* Runs all validations and returns an array of
* error messages
*/
public function errors(): 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 ($validation instanceof Closure) {
try {
$validation->call($this, $value);
} catch (Exception $e) {
$errors[$key] = $e->getMessage();
}
}
}
if (
empty($this->validate) === false &&
($this->isEmpty() === false || $this->isRequired() === true)
) {
$rules = A::wrap($this->validate);
$errors = [
...$errors,
...V::errors($value, $rules)
];
}
return $errors;
}
/**
* Checks if the field is required
*/
public function isRequired(): bool
{
return $this->required;
}
/**
* Checks if the field is invalid
*/
public function isInvalid(): bool
{
return $this->errors() !== [];
}
/**
* Checks if the field is valid
*/
public function isValid(): bool
{
return $this->errors() === [];
}
public function required(): bool
{
return $this->required;
}
protected function setRequired(bool $required = false): void
{
$this->required = $required;
}
/**
* Defines all validation rules
*/
protected function validations(): array
{
return [];
}
}

View file

@ -0,0 +1,228 @@
<?php
namespace Kirby\Form\Mixin;
use Kirby\Cms\Language;
use ReflectionProperty;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Value
{
/**
* Default value for the field, which will be used when a page/file/user is created
*/
protected mixed $default = null;
/**
* The value of the field
*/
protected mixed $value = null;
/**
* @deprecated 5.0.0 Use `::toStoredValue()` instead to receive
* the value in the format that will be needed for content files.
*
* If you need to get the value with the default as fallback, you should use
* the fill method first `$field->fill($field->default())->toStoredValue()`
*/
public function data(bool $default = false): mixed
{
if ($default === true && $this->isEmpty() === true) {
$this->fill($this->default());
}
return $this->toStoredValue();
}
/**
* Returns the default value of the field
*/
public function default(): mixed
{
if (is_string($this->default) === false) {
return $this->default;
}
return $this->model->toString($this->default);
}
/**
* Returns the fallback value when the field should be empty
*/
public function emptyValue(): mixed
{
return (new ReflectionProperty($this, 'value'))->getDefaultValue();
}
/**
* Sets a new value for the field
*/
public function fill(mixed $value): static
{
$this->value = $value;
return $this;
}
/**
* Checks if the field has a value
*/
public function hasValue(): bool
{
return true;
}
/**
* Checks if the field is empty
*/
public function isEmpty(): bool
{
return $this->isEmptyValue($this->toFormValue());
}
/**
* Checks if the given value is considered empty
*/
public function isEmptyValue(mixed $value = null): bool
{
return in_array($value, [null, '', []], true);
}
/**
* Checks if the field can be stored in the given language.
*/
public function isStorable(Language $language): bool
{
// the field cannot be stored at all if it has no value
if ($this->hasValue() === false) {
return false;
}
// the field cannot be translated into the given language
if ($this->isTranslatable($language) === false) {
return false;
}
// We don't need to check if the field is disabled.
// A disabled field can still have a value and that value
// should still be stored. But that value must not be changed
// on submit. That's why we check for the disabled state
// in the isSubmittable method.
return true;
}
/**
* A field might have a value, but can still not be submitted
* because it is disabled, not translatable into the given
* language or not active due to a `when` rule.
*/
public function isSubmittable(Language $language): bool
{
if ($this->hasValue() === false) {
return false;
}
if ($this->isTranslatable($language) === false) {
return false;
}
return true;
}
/**
* Checks if the field needs a value before being saved;
* this is the case if all of the following requirements are met:
* - The field has a value
* - The field is required
* - The field is currently empty
* - The field is not currently inactive because of a `when` rule
*/
protected function needsValue(): bool
{
if (
$this->hasValue() === false ||
$this->isRequired() === false ||
$this->isEmpty() === false ||
$this->isActive() === false
) {
return false;
}
return true;
}
/**
* Checks if the field is saveable
* @deprecated 5.0.0 Use `::hasValue()` instead
*/
public function save(): bool
{
return $this->hasValue();
}
protected function setDefault(mixed $default = null): void
{
$this->default = $default;
}
/**
* Submits a new value for the field.
* Fields can overwrite this method to provide custom
* submit logic. This is useful if the field component
* sends data that needs to be processed before being
* stored.
*
* @since 5.0.0
*/
public function submit(mixed $value): static
{
return $this->fill($value);
}
/**
* Returns the value of the field in a format to be used in forms
* (e.g. used as data for Panel Vue components)
*/
public function toFormValue(): mixed
{
if ($this->hasValue() === false) {
return null;
}
return $this->value;
}
/**
* Returns the value of the field in a format
* to be stored by our storage classes
*/
public function toStoredValue(): mixed
{
return $this->toFormValue();
}
/**
* Returns the value of the field if it has a value
* otherwise it returns null
*
* @see `self::toFormValue()`
* @todo might get deprecated or reused later. Use `self::toFormValue()` instead.
*
* If you need the form value with the default as fallback, you should use
* the fill method first `$field->fill($field->default())->toFormValue()`
*/
public function value(bool $default = false): mixed
{
if ($default === true && $this->isEmpty() === true) {
$this->fill($this->default());
}
return $this->toFormValue();
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Kirby\Form\Mixin;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait When
{
/**
* Conditions when the field will be shown
*
* @since 3.1.0
*/
protected array|null $when = null;
/**
* Checks if the field is currently active
* or hidden because of a `when` condition
*/
public function isActive(): bool
{
if ($this->when === null || $this->when === []) {
return true;
}
$siblings = $this->siblings();
foreach ($this->when as $field => $value) {
$field = $siblings->get($field);
$input = $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 ($input !== $value) {
return false;
}
}
return true;
}
protected function setWhen(array|null $when = null): void
{
$this->when = $when;
}
public function when(): array|null
{
return $this->when;
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Kirby\Form\Mixin;
/**
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
trait Width
{
/**
* The width of the field in the field grid.
* Available widths: `1/1`, `1/2`, `1/3`, `1/4`, `2/3`, `3/4`
*/
protected string|null $width;
protected function setWidth(string|null $width = null): void
{
$this->width = $width;
}
public function width(): string
{
return $this->width ?? '1/1';
}
}

View file

@ -0,0 +1,279 @@
<?php
namespace Kirby\Form;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\V;
/**
* Often used validation rules for fields
*
* @package Kirby Form
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Validations
{
/**
* Validates if the field value is boolean
*
* @param \Kirby\Form\Field|\Kirby\Form\FieldClass $field
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function boolean($field, $value): bool
{
if ($field->isEmptyValue($value) === false) {
if (is_bool($value) === false) {
throw new InvalidArgumentException(
key: 'validation.boolean'
);
}
}
return true;
}
/**
* Validates if the field value is valid date
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function date(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmptyValue($value) === false) {
if (V::date($value) !== true) {
throw new InvalidArgumentException(
message: V::message('date', $value)
);
}
}
return true;
}
/**
* Validates if the field value is valid email
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function email(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmptyValue($value) === false) {
if (V::email($value) === false) {
throw new InvalidArgumentException(
message: V::message('email', $value)
);
}
}
return true;
}
/**
* Validates if the field value is maximum
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function max(Field|FieldClass $field, mixed $value): bool
{
if (
$field->isEmptyValue($value) === false &&
$field->max() !== null
) {
if (V::max($value, $field->max()) === false) {
throw new InvalidArgumentException(
message: V::message('max', $value, $field->max())
);
}
}
return true;
}
/**
* Validates if the field value is max length
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function maxlength(Field|FieldClass $field, mixed $value): bool
{
if (
$field->isEmptyValue($value) === false &&
$field->maxlength() !== null
) {
if (V::maxLength($value, $field->maxlength()) === false) {
throw new InvalidArgumentException(
message: V::message('maxlength', $value, $field->maxlength())
);
}
}
return true;
}
/**
* Validates if the field value is minimum
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function min(Field|FieldClass $field, mixed $value): bool
{
if (
$field->isEmptyValue($value) === false &&
$field->min() !== null
) {
if (V::min($value, $field->min()) === false) {
throw new InvalidArgumentException(
message: V::message('min', $value, $field->min())
);
}
}
return true;
}
/**
* Validates if the field value is min length
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function minlength(Field|FieldClass $field, mixed $value): bool
{
if (
$field->isEmptyValue($value) === false &&
$field->minlength() !== null
) {
if (V::minLength($value, $field->minlength()) === false) {
throw new InvalidArgumentException(
message: V::message('minlength', $value, $field->minlength())
);
}
}
return true;
}
/**
* Validates if the field value matches defined pattern
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function pattern(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmptyValue($value) === false) {
if ($pattern = $field->pattern()) {
// ensure that that pattern needs to match the whole
// input value from start to end, not just a partial match
// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern#overview
$pattern = '^(?:' . $pattern . ')$';
if (V::match($value, '/' . $pattern . '/i') === false) {
throw new InvalidArgumentException(
message: V::message('match')
);
}
}
}
return true;
}
/**
* Validates if the field value is required
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function required(Field|FieldClass $field, mixed $value): bool
{
if (
$field->hasValue() === true &&
$field->isRequired() === true &&
$field->isEmptyValue($value) === true
) {
throw new InvalidArgumentException(
key: 'validation.required'
);
}
return true;
}
/**
* Validates if the field value is in defined options
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function option(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmptyValue($value) === false) {
$values = array_column($field->options(), 'value');
if (in_array($value, $values, true) !== true) {
throw new InvalidArgumentException(
key: 'validation.option'
);
}
}
return true;
}
/**
* Validates if the field values is in defined options
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function options(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmptyValue($value) === false) {
$values = array_column($field->options(), 'value');
foreach ($value as $val) {
if (in_array($val, $values, true) === false) {
throw new InvalidArgumentException(
key: 'validation.option'
);
}
}
}
return true;
}
/**
* Validates if the field value is valid time
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function time(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmptyValue($value) === false) {
if (V::time($value) !== true) {
throw new InvalidArgumentException(
message: V::message('time', $value)
);
}
}
return true;
}
/**
* Validates if the field value is valid url
*
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function url(Field|FieldClass $field, mixed $value): bool
{
if ($field->isEmptyValue($value) === false) {
if (V::url($value) === false) {
throw new InvalidArgumentException(
message: V::message('url', $value)
);
}
}
return true;
}
}