adrema/app/Form/Fields/DateField.php

77 lines
1.6 KiB
PHP
Raw Normal View History

2023-12-30 01:00:58 +01:00
<?php
namespace App\Form\Fields;
use Faker\Generator;
class DateField extends Field
{
2024-02-06 01:45:25 +01:00
public string $name;
public string $key;
public bool $required;
public bool $maxToday;
2023-12-30 01:00:58 +01:00
public static function name(): string
{
return 'Datum';
}
public static function meta(): array
{
return [
['key' => 'required', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'],
['key' => 'max_today', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Nur daten bis heute erlauben'],
];
}
2024-02-03 17:51:27 +01:00
public static function default(): ?string
2023-12-30 01:00:58 +01:00
{
2024-02-03 17:51:27 +01:00
return null;
2023-12-30 01:00:58 +01:00
}
public static function fake(Generator $faker): array
{
return [
'required' => $faker->boolean(),
'max_today' => $faker->boolean(),
];
}
2024-02-06 01:45:25 +01:00
/**
* @inheritdoc
*/
public function getRegistrationRules(): array
{
$rules = [$this->required ? 'required' : 'nullable'];
$rules[] = 'date';
if ($this->maxToday) {
$rules[] = 'before_or_equal:' . now()->format('Y-m-d');
}
return [$this->key => $rules];
}
/**
* @inheritdoc
*/
public function getRegistrationAttributes(): array
{
return [
$this->key => $this->name,
];
}
/**
* @inheritdoc
*/
public function getRegistrationMessages(): array
{
return [
$this->key . '.before_or_equal' => $this->name . ' muss ein Datum vor oder gleich dem ' . now()->format('d.m.Y') . ' sein.',
];
}
2023-12-30 01:00:58 +01:00
}