75 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Form\Fields;
 | |
| 
 | |
| use App\Form\Models\Form;
 | |
| use App\Form\Models\Participant;
 | |
| use Faker\Generator;
 | |
| use Illuminate\Validation\Rule;
 | |
| 
 | |
| class DropdownField extends Field
 | |
| {
 | |
|     public bool $required;
 | |
|     /** @var array<int, string> */
 | |
|     public array $options;
 | |
| 
 | |
|     public static function name(): string
 | |
|     {
 | |
|         return 'Dropdown';
 | |
|     }
 | |
| 
 | |
|     public static function meta(): array
 | |
|     {
 | |
|         return [
 | |
|             ['key' => 'options', 'default' => [], 'rules' => ['options' => 'present|array', 'options.*' => 'string'], 'label' => 'Optionen'],
 | |
|             ['key' => 'required', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'],
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     public static function default()
 | |
|     {
 | |
|         return null;
 | |
|     }
 | |
| 
 | |
|     public static function fake(Generator $faker): array
 | |
|     {
 | |
|         return [
 | |
|             'options' => $faker->words(4),
 | |
|             'required' => $faker->boolean(),
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @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 [];
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @inheritdoc
 | |
|      */
 | |
|     public function afterRegistration(Form $form, Participant $participant, array $input): void
 | |
|     {
 | |
|     }
 | |
| }
 |