wi-events/components/EventForm.php

68 lines
2.0 KiB
PHP
Raw Normal View History

2023-01-07 16:10:18 +01:00
<?php
namespace Zoomyboy\Event\Components;
use Cms\Classes\ComponentBase;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Lang;
use Input;
use Winter\Storm\Support\Facades\Validator;
2023-01-07 18:13:03 +01:00
use Zoomyboy\Event\Models\Participant;
2023-01-07 16:10:18 +01:00
class EventForm extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'EventForm Component',
'description' => 'No description provided yet...',
];
}
public function defineProperties()
{
return [];
}
public function onSubmit(): JsonResponse
{
2023-01-07 17:51:26 +01:00
$rules = [
2023-01-07 16:10:18 +01:00
'activity' => 'required|max:255',
'gender' => 'required|max:255',
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'birthday' => 'required|date|before_or_equal:'.now()->format('Y-m-d'),
'address' => 'required|max:255',
'zip' => 'required|numeric',
'location' => 'required|max:255',
'phone' => 'required|max:255',
'email' => 'required|max:255|email',
'agegroup' => 'required|max:255',
'group' => 'nullable|max:255',
'agegroup_leader' => 'nullable|max:255',
'emergency_phone' => 'required|max:255',
'food_preferences' => 'array',
'misc' => '',
'foto' => '',
2023-01-07 17:51:26 +01:00
];
if ('Orga' === Input::get('activity')) {
$rules['vorteam'] = 'in:Ja,Nein';
}
$validator = Validator::make(Input::all(), $rules, [
'vorteam.in' => 'Bitte gebe an, ob du am Vorteam teilnehmen willst.',
], Lang::get('zoomyboy.event::validation.attributes'));
2023-01-07 16:10:18 +01:00
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
2023-01-07 18:13:03 +01:00
Participant::create(array_merge($validator->validated(), [
'vorteam' => 'Ja' === Input::get('vorteam'),
]));
return response()->json([], 201);
2023-01-07 16:10:18 +01:00
}
}