Add minmax for checkboxes
continuous-integration/drone/push Build is failing Details

This commit is contained in:
philipp lang 2024-06-10 17:46:41 +02:00
parent df202d9a9a
commit 6ae61655de
4 changed files with 123 additions and 1 deletions

View File

@ -13,6 +13,8 @@ class CheckboxesField extends Field
{
/** @var array<int, string> */
public array $options;
public ?int $min;
public ?int $max;
public static function name(): string
{
@ -23,6 +25,8 @@ class CheckboxesField extends Field
{
return [
['key' => 'options', 'default' => [], 'rules' => ['options' => 'array', 'options.*' => 'required|string'], 'label' => 'Optionen'],
['key' => 'min', 'default' => null, 'rules' => ['min' => 'present'], 'label' => 'minimale Anzahl'],
['key' => 'max', 'default' => null, 'rules' => ['max' => 'present'], 'label' => 'maximale Anzahl'],
];
}
@ -35,6 +39,8 @@ class CheckboxesField extends Field
{
return [
'options' => $faker->words(4),
'min' => null,
'max' => null,
];
}
@ -43,8 +49,18 @@ class CheckboxesField extends Field
*/
public function getRegistrationRules(Form $form): array
{
$globalRules = ['array'];
if ($this->min > 0) {
$globalRules[] = 'min:' . $this->min;
}
if ($this->max > 0) {
$globalRules[] = 'max:' . $this->max;
}
return [
$this->key => 'array',
$this->key => $globalRules,
$this->key . '.*' => ['string', Rule::in($this->options)],
];
}

View File

@ -0,0 +1,64 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
foreach (DB::table('forms')->get() as $event) {
$config = json_decode($event->config);
$config->sections = array_map(function ($section) {
/** @var Collection<int, mixed> */
$fields = $section->fields;
$section->fields = collect($fields)->map(function ($field) {
if ($field->type === 'CheckboxesField') {
$field->min = 0;
$field->max = 0;
}
return $field;
})->all();
return $section;
}, $config->sections);
DB::table('forms')->where('id', $event->id)->update(['config' => json_encode($config)]);
}
foreach (DB::table('formtemplates')->get() as $event) {
$config = json_decode($event->config);
$config->sections = array_map(function ($section) {
/** @var Collection<int, mixed> */
$fields = $section->fields;
$section->fields = collect($fields)->map(function ($field) {
if ($field->type === 'CheckboxesField') {
$field->min = 0;
$field->max = 0;
}
return $field;
})->all();
return $section;
}, $config->sections);
DB::table('formtemplates')->where('id', $event->id)->update(['config' => json_encode($config)]);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
};

View File

@ -18,6 +18,24 @@
></ui-action-button>
</div>
<ui-icon-button icon="plus" @click="$emit('update:modelValue', {...modelValue, options: addOption(modelValue.options)})">Option einfügen</ui-icon-button>
<f-text
id="min"
type="number"
size="sm"
name="min"
label="Minimale Anzahl Elemente"
:model-value="modelValue.min"
@update:model-value="$emit('update:modelValue', {...modelValue, min: $event})"
></f-text>
<f-text
id="max"
type="number"
size="sm"
name="max"
label="Maximale Anzahl Elemente"
:model-value="modelValue.max"
@update:model-value="$emit('update:modelValue', {...modelValue, max: $event})"
></f-text>
</div>
</template>

View File

@ -200,6 +200,30 @@ class FormRegisterActionTest extends FormTestCase
null,
];
yield [
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(2),
['letter' => ['A', 'B', 'C']],
['letter' => 'Buchstabe darf maximal 2 Elemente haben.'],
];
yield [
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(2)->max(0),
['letter' => ['A']],
['letter' => 'Buchstabe muss mindestens 2 Elemente haben.'],
];
yield [
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(1)->max(0),
['letter' => []],
['letter' => 'Buchstabe muss mindestens 1 Elemente haben.'],
];
yield [
$this->checkboxesField('letter')->name('Buchstabe')->options(['A', 'B', 'C', 'D'])->min(0)->max(1),
['letter' => ['A', 'B']],
['letter' => 'Buchstabe darf maximal 1 Elemente haben.'],
];
yield [
$this->checkboxField('data')->name('Datenschutz')->required(false),
['data' => 5],