2023-12-26 00:44:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Fields;
|
|
|
|
|
2023-12-26 20:06:57 +01:00
|
|
|
use Faker\Generator;
|
2024-02-06 01:45:25 +01:00
|
|
|
use Illuminate\Validation\Rule;
|
2023-12-26 20:06:57 +01:00
|
|
|
|
2023-12-26 00:44:49 +01:00
|
|
|
class RadioField extends Field
|
|
|
|
{
|
2024-02-06 01:45:25 +01:00
|
|
|
public bool $required;
|
|
|
|
/** @var array<int, string> */
|
|
|
|
public array $options;
|
|
|
|
|
2023-12-26 00:44:49 +01:00
|
|
|
public static function name(): string
|
|
|
|
{
|
|
|
|
return 'Radio';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function meta(): array
|
|
|
|
{
|
|
|
|
return [
|
2023-12-26 20:24:57 +01:00
|
|
|
['key' => 'options', 'default' => [], 'rules' => ['options' => 'present|array', 'options.*' => 'required|string'], 'label' => 'Optionen'],
|
2023-12-26 20:06:57 +01:00
|
|
|
['key' => 'required', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'],
|
2023-12-26 00:44:49 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function default()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-26 20:06:57 +01:00
|
|
|
|
|
|
|
public static function fake(Generator $faker): array
|
|
|
|
{
|
2023-12-27 22:39:23 +01:00
|
|
|
return [
|
|
|
|
'options' => $faker->words(4),
|
|
|
|
'required' => $faker->boolean(),
|
|
|
|
];
|
2023-12-26 20:06:57 +01:00
|
|
|
}
|
2024-02-06 01:45:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getRegistrationRules(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
$this->key => $this->required ? ['required', 'string', Rule::in($this->options)] : ['nullable', 'string', Rule::in($this->options)],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getRegistrationAttributes(): array
|
|
|
|
{
|
|
|
|
return [$this->key => $this->name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getRegistrationMessages(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2023-12-26 00:44:49 +01:00
|
|
|
}
|