LMIV-SNACKAUTOMAT/app/Http/Requests/ProductRequest.php

100 lines
4.0 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'price' => 'required|numeric|min:0',
'image' => 'nullable|file|mimes:jpeg,jpg,png,gif,webp|max:2048',
'barcode' => 'nullable|string|max:255',
'ingredients' => 'nullable|string',
'allergens' => 'nullable|string',
'net_weight' => 'nullable|numeric|min:0',
'origin_country' => 'nullable|string|max:255',
'manufacturer' => 'nullable|string|max:255',
'distributor' => 'nullable|string|max:255',
'energy_kj' => 'nullable|numeric|min:0',
'energy_kcal' => 'nullable|numeric|min:0',
'fat' => 'nullable|numeric|min:0',
'saturated_fat' => 'nullable|numeric|min:0',
'carbohydrates' => 'nullable|numeric|min:0',
'sugar' => 'nullable|numeric|min:0',
'protein' => 'nullable|numeric|min:0',
'salt' => 'nullable|numeric|min:0',
'storage_instructions' => 'nullable|string',
'usage_instructions' => 'nullable|string',
'best_before_date' => 'nullable|date',
'lot_number' => 'nullable|string|max:255',
];
}
/**
* Get custom validation messages.
*/
public function messages(): array
{
return [
'name.required' => 'Der Produktname ist erforderlich.',
'name.max' => 'Der Produktname darf maximal :max Zeichen lang sein.',
'price.required' => 'Der Preis ist erforderlich.',
'price.numeric' => 'Der Preis muss eine Zahl sein.',
'price.min' => 'Der Preis muss mindestens :min sein.',
'image.file' => 'Das Bild muss eine gültige Datei sein.',
'image.mimes' => 'Das Bild muss vom Typ JPEG, JPG, PNG, GIF oder WebP sein.',
'image.max' => 'Das Bild darf maximal :max KB groß sein.',
'energy_kj.numeric' => 'Der Energiegehalt (kJ) muss eine Zahl sein.',
'energy_kcal.numeric' => 'Der Energiegehalt (kcal) muss eine Zahl sein.',
'fat.numeric' => 'Der Fettgehalt muss eine Zahl sein.',
'saturated_fat.numeric' => 'Der Gehalt an gesättigten Fettsäuren muss eine Zahl sein.',
'carbohydrates.numeric' => 'Der Kohlenhydratgehalt muss eine Zahl sein.',
'sugar.numeric' => 'Der Zuckergehalt muss eine Zahl sein.',
'protein.numeric' => 'Der Proteingehalt muss eine Zahl sein.',
'salt.numeric' => 'Der Salzgehalt muss eine Zahl sein.',
'net_weight.numeric' => 'Das Nettogewicht muss eine Zahl sein.',
'best_before_date.date' => 'Das Mindesthaltbarkeitsdatum muss ein gültiges Datum sein.',
];
}
/**
* Configure the validator instance.
*/
public function withValidator($validator)
{
$validator->after(function ($validator) {
if ($this->hasFile('image')) {
$file = $this->file('image');
$mimeType = $file->getMimeType();
// Explizite WebP-Überprüfung
if ($mimeType === 'image/webp' || $file->getClientOriginalExtension() === 'webp') {
// WebP ist erlaubt
return;
}
// Standard-Bildtypen prüfen
$allowedMimes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'];
if (!in_array($mimeType, $allowedMimes)) {
$validator->errors()->add('image', 'Das Bildformat wird nicht unterstützt. Erlaubt sind: JPEG, PNG, GIF, WebP.');
}
}
});
}
}