Add more form fields

This commit is contained in:
philipp lang 2023-12-31 21:46:52 +01:00
parent 4776c0d71b
commit d3ad48ed30
6 changed files with 58 additions and 2 deletions

View File

@ -22,6 +22,12 @@ class FormStoreAction
...$this->globalRules(),
'description' => 'required|string',
'excerpt' => 'required|string|max:120',
'from' => 'required|date',
'to' => 'required|date',
'registration_from' => 'present|nullable|date',
'registration_until' => 'present|nullable|date',
'mail_top' => 'nullable|string',
'mail_bottom' => 'nullable|string',
];
}
@ -33,6 +39,18 @@ class FormStoreAction
return Form::create($attributes);
}
/**
* @return array<string, mixed>
*/
public function getValidationAttributes(): array
{
return [
...$this->globalValidationAttributes(),
'from' => 'Start',
'to' => 'Ende',
];
}
public function asController(ActionRequest $request): JsonResponse
{
$this->handle($request->validated());

View File

@ -33,7 +33,7 @@ trait HasValidation
/**
* @return array<string, mixed>
*/
public function getValidationAttributes(): array
public function globalValidationAttributes(): array
{
return [
'config.sections.*.name' => 'Sektionsname',

View File

@ -14,4 +14,7 @@ class Form extends Model
public $casts = [
'config' => 'json',
];
/** @var array<int, string> */
public $dates = ['from', 'to', 'registration_from', 'registration_until'];
}

View File

@ -26,6 +26,12 @@ return new class extends Migration
$table->text('description');
$table->text('excerpt');
$table->json('config');
$table->dateTime('from');
$table->dateTime('to');
$table->dateTime('registration_from')->nullable();
$table->dateTime('registration_until')->nullable();
$table->text('mail_top')->nullable();
$table->text('mail_bottom')->nullable();
$table->timestamps();
});
}

View File

@ -21,6 +21,12 @@ class FormRequest extends RequestFactory
'description' => $this->faker->text(),
'excerpt' => $this->faker->words(10, true),
'config' => ['sections' => []],
'from' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'to' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'registration_from' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'registration_until' => $this->faker->dateTime()->format('Y-m-d H:i:s'),
'mail_top' => $this->faker->text(),
'mail_bottom' => $this->faker->text(),
];
}
@ -37,6 +43,6 @@ class FormRequest extends RequestFactory
*/
public function __call(string $method, $args): self
{
return $this->state([$method => $args[0]]);
return $this->state([str($method)->snake()->toString() => $args[0]]);
}
}

View File

@ -22,6 +22,9 @@ class FormStoreActionTest extends TestCase
->name('formname')
->description('lala ggg')
->excerpt('avff')
->registrationFrom('2023-05-04 01:00:00')->registrationUntil('2023-07-07 01:00:00')->from('2023-07-07 03:00')->to('2023-07-08 05:00')
->mailTop('Guten Tag')
->mailBottom('Viele Grüße')
->sections([FormtemplateSectionRequest::new()->name('sname')->fields([FormtemplateFieldRequest::new()])])
->fake();
@ -32,14 +35,34 @@ class FormStoreActionTest extends TestCase
$this->assertEquals('formname', $form->name);
$this->assertEquals('avff', $form->excerpt);
$this->assertEquals('lala ggg', $form->description);
$this->assertEquals('Guten Tag', $form->mail_top);
$this->assertEquals('Viele Grüße', $form->mail_bottom);
$this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i'));
$this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i'));
$this->assertEquals('2023-07-07 03:00', $form->from->format('Y-m-d H:i'));
$this->assertEquals('2023-07-08 05:00', $form->to->format('Y-m-d H:i'));
Event::assertDispatched(Succeeded::class, fn (Succeeded $event) => $event->message === 'Formular gespeichert.');
}
public function testRegistrationDatesCanBeNull(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$this->postJson(route('form.store'), FormRequest::new()->registrationFrom(null)->registrationUntil(null)->create())->assertOk();
$this->assertDatabaseHas('forms', [
'registration_until' => null,
'registration_from' => null,
]);
}
public function validationDataProvider(): Generator
{
yield [FormRequest::new()->name(''), ['name' => 'Name ist erforderlich.']];
yield [FormRequest::new()->excerpt(''), ['excerpt' => 'Auszug ist erforderlich.']];
yield [FormRequest::new()->description(''), ['description' => 'Beschreibung ist erforderlich.']];
yield [FormRequest::new()->state(['from' => null]), ['from' => 'Start ist erforderlich']];
yield [FormRequest::new()->state(['to' => null]), ['to' => 'Ende ist erforderlich']];
}
/**