103 lines
3.9 KiB
PHP
103 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Enums\BreederCivilty;
|
|
use App\Models\CafelEggBreeder;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
|
|
|
class StoreEleveurRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
$this->merge([
|
|
'code_inuav' => explode(',', $this->code_inuav)
|
|
]);
|
|
return auth()->check();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
// TODO: add validation rules using CAFEL API standards.
|
|
return [
|
|
'code_cafel' => 'nullable|string|min:1|max:100|unique:eleveurs,code_cafel',
|
|
'code_ede' => 'required|string|min:1|max:100|unique:eleveurs,code_ede',
|
|
'civilite' => ['required', new EnumIsValidRule(BreederCivilty::class)],
|
|
'nom' => 'required|string|min:1|max:255',
|
|
'prenom' => 'required|string|min:1|max:255',
|
|
'naissance' => 'required|date_format:Y-m-d',
|
|
'cp' => 'required|numeric|digits:5',
|
|
'ville' => 'required|string|min:1|max:255',
|
|
'elv_date_debut' => 'nullable|date_format:Y-m-d',
|
|
'code_inuav' => [
|
|
'array', function ($attribute, $value, $fail) {
|
|
if (\strlen(\json_encode($value)) > 255) {
|
|
$fail("$attribute : il n'\est pas possible de renseigner autant de code INUAV");
|
|
}
|
|
}
|
|
],
|
|
'code_inuav.*' => [
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
if (strpos(trim($value), ' ') !== false) {
|
|
$fail(trans('Les espaces ne sont pas autorisés dans les codes INUAV'));
|
|
}
|
|
if (count($this->code_inuav) > count(array_unique($this->code_inuav))) {
|
|
$fail(trans('Les codes INUAVS doinvent être uniques'));
|
|
}
|
|
$q = CafelEggBreeder::query();
|
|
$q->where(function ($q) {
|
|
foreach ($this->code_inuav as $inuav) {
|
|
$q->orWhere('code_inuav', 'LIKE', "%$inuav%");
|
|
}
|
|
});
|
|
// * Update condition
|
|
if ($this->eleveur) {
|
|
$q->where('id', '!=', $this->eleveur->id);
|
|
}
|
|
if ($q->count()) {
|
|
$fail(trans('Les codes INUAVS doinvent être uniques'));
|
|
}
|
|
}
|
|
],
|
|
'exploitation' => 'nullable|string',
|
|
'aboutyou' => 'nullable|string',
|
|
'vocation' => 'nullable|string',
|
|
'likeinjob' => 'nullable|string',
|
|
'loue_means' => 'nullable|string',
|
|
'photo1' => 'sometimes|mimetypes:image/jpeg|max:1000',
|
|
'photo2' => 'sometimes|mimetypes:image/jpeg|max:1000',
|
|
'marquage_oeuf' => 'required|string|max:12',
|
|
'marquage_b1' => 'required|string|max:14',
|
|
'marquage_b2' => 'required|string|max:14',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the error messages for the defined validation rules.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function messages()
|
|
{
|
|
return [
|
|
'photo1.max' => 'La Photo 1 ne doit pas dépasser 500 Ko',
|
|
'photo1.uploaded' => 'La Photo 1 ne doit pas dépasser 500 Ko',
|
|
'photo1.mimetypes' => 'La Photo 1 doit être un jpg',
|
|
'photo2.max' => 'La Photo 2 ne doit pas dépasser 500 Ko',
|
|
'photo2.mimetypes' => 'La Photo 2 doit être un jpg',
|
|
'photo2.uploaded' => 'La Photo 2 ne doit pas dépasser 500 Ko',
|
|
];
|
|
}
|
|
}
|