CustomScheduleRequest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. class CustomScheduleRequest extends FormRequest
  5. {
  6. /**
  7. * Determine if the user is authorized to make this request.
  8. */
  9. public function authorize(): bool
  10. {
  11. return true;
  12. }
  13. /**
  14. * Get the validation rules that apply to the request.
  15. *
  16. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  17. */
  18. public function rules(): array
  19. {
  20. $rules = [
  21. 'client_id' => 'required|exists:clients,id',
  22. 'address_id' => 'required|exists:addresses,id',
  23. 'address_type' => 'required|in:home,commercial',
  24. 'service_type_id' => 'required|exists:service_types,id',
  25. 'description' => 'nullable|string',
  26. 'min_price' => 'required|numeric|min:0',
  27. 'max_price' => 'required|numeric|min:0|gte:min_price',
  28. 'offers_meal' => 'nullable|boolean',
  29. 'date' => 'required|date|after_or_equal:today',
  30. 'period_type' => 'required|in:2,4,6,8',
  31. 'start_time' => 'required|date_format:H:i:s',
  32. 'end_time' => 'required|date_format:H:i:s|after:start_time',
  33. 'quantity' => 'nullable|integer|min:1|max:10',
  34. 'speciality_ids' => 'nullable|array',
  35. 'speciality_ids.*' => 'exists:specialities,id',
  36. ];
  37. if ($this->isMethod('PUT') || $this->isMethod('PATCH')) {
  38. foreach ($rules as $key => $rule) {
  39. if (str_starts_with($rule, 'required')) {
  40. $rules[$key] = 'sometimes|' . substr($rule, 9);
  41. }
  42. }
  43. }
  44. return $rules;
  45. }
  46. /**
  47. * Get custom messages for validator errors.
  48. */
  49. public function messages(): array
  50. {
  51. return [
  52. 'max_price.gte' => 'O preço máximo deve ser maior ou igual ao preço mínimo.',
  53. 'date.after_or_equal' => 'A data deve ser hoje ou uma data futura.',
  54. 'end_time.after' => 'O horário de término deve ser posterior ao horário de início.',
  55. ];
  56. }
  57. }