Add empty option for group field
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

This commit is contained in:
philipp lang 2024-04-18 01:03:30 +02:00
parent 913e45eb75
commit fd1e0bb9e1
4 changed files with 42 additions and 5 deletions

View File

@ -16,6 +16,8 @@ class GroupField extends Field
public bool $required;
public ?string $parentField = null;
public ?int $parentGroup = null;
public bool $hasEmptyOption;
public string $emptyOptionValue;
public static function name(): string
{
@ -28,6 +30,8 @@ class GroupField extends Field
['key' => 'required', 'default' => true, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'],
['key' => 'parent_field', 'default' => null, 'rules' => ['parent_field' => 'present|nullable|string'], 'label' => 'Übergeordnetes Feld'],
['key' => 'parent_group', 'default' => null, 'rules' => ['parent_group' => ['present', 'nullable', Rule::in(Group::pluck('id')->toArray())]], 'label' => 'Übergeordnete Gruppierung'],
['key' => 'has_empty_option', 'default' => false, 'rules' => ['has_empty_option' => ['present', 'boolean']], 'label' => 'Leere Option erlauben'],
['key' => 'empty_option_value', 'default' => '', 'rules' => ['empty_option_value' => ['present', 'string']], 'label' => 'Leere Option'],
];
}
@ -42,6 +46,8 @@ class GroupField extends Field
'required' => $faker->boolean(),
'parent_field' => null,
'parent_group' => null,
'has_empty_option' => $faker->boolean(),
'empty_option_value' => '',
];
}
@ -56,11 +62,11 @@ class GroupField extends Field
$rules[] = 'integer';
if ($this->parentGroup) {
$rules[] = Rule::in(Group::find($this->parentGroup)->children()->pluck('id'));
$rules[] = Rule::in(Group::find($this->parentGroup)->children()->pluck('id')->push(-1));
}
if ($this->parentField && request()->input($this->parentField)) {
$rules[] = Rule::in(Group::find(request()->input($this->parentField))->children()->pluck('id'));
$rules[] = Rule::in(Group::find(request()->input($this->parentField))->children()->pluck('id')->push(-1));
}
return [$this->key => $rules];
@ -84,7 +90,7 @@ class GroupField extends Field
public function getPresenter(): Presenter
{
return app(GroupPresenter::class);
return app(GroupPresenter::class)->field($this);
}
/**

View File

@ -2,17 +2,30 @@
namespace App\Form\Presenters;
use App\Form\Fields\GroupField;
use App\Group;
use Carbon\Carbon;
class GroupPresenter extends Presenter
{
private GroupField $field;
public function field(GroupField $field): self
{
$this->field = $field;
return $this;
}
/**
* @param ?int $value
*/
public function present($value): string
{
if ($value === -1) {
return $this->field->emptyOptionValue;
}
if (!$value) {
return '';
}

@ -1 +1 @@
Subproject commit 31b03b69ed3d57888cd82d1bfedcd0cb2b9cdc3c
Subproject commit 42a2f6cb08cdac743fe7c06c719ddf536bb58244

View File

@ -8,6 +8,24 @@
inline
@update:modelValue="$emit('update:modelValue', {...modelValue, required: $event})"
></f-switch>
<f-switch
id="has_empty_option"
v-model="modelValue.has_empty_option"
label="Leere Option erlauben"
size="sm"
name="has_empty_option"
inline
@update:modelValue="$emit('update:modelValue', {...modelValue, has_empty_option: $event})"
></f-switch>
<f-text
v-if="modelValue.has_empty_option"
id="empty_option_value"
v-model="modelValue.empty_option_value"
label="Wert der leeren Option"
size="sm"
name="empty_option_value"
@update:modelValue="$emit('update:modelValue', {...modelValue, empty_option_value: $event})"
></f-text>
<f-select
id="parent_field"
:options="fieldOptions"