AddressRequest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. class AddressRequest extends FormRequest
  5. {
  6. public function rules(): array
  7. {
  8. $rules = [
  9. 'source' => 'sometimes|string|in:provider,client',
  10. 'source_id' => 'sometimes|integer|min:1',
  11. 'zip_code' => [
  12. 'sometimes',
  13. 'string',
  14. 'regex:/^[0-9]{8}$/',
  15. ],
  16. 'address' => 'sometimes|string',
  17. 'number' => 'nullable|string|max:20',
  18. 'district' => 'nullable|string|max:255',
  19. 'has_complement' => 'sometimes|boolean',
  20. 'complement' => 'nullable|string|max:255',
  21. 'nickname' => 'nullable|string|max:255',
  22. 'instructions' => 'nullable|string',
  23. 'city_id' => 'nullable|integer|exists:cities,id',
  24. 'state_id' => 'nullable|integer|exists:states,id',
  25. 'address_type' => 'sometimes|in:home,commercial,other',
  26. 'is_primary' => 'sometimes|boolean',
  27. 'latitude' => 'nullable|numeric|between:-90,90',
  28. 'longitude' => 'nullable|numeric|between:-180,180',
  29. ];
  30. if ($this->isMethod('post')) {
  31. $rules['source'] = 'required|string|in:provider,client';
  32. $rules['source_id'] = [
  33. 'required',
  34. 'integer',
  35. 'min:1',
  36. ];
  37. $rules['zip_code'] = [
  38. 'required',
  39. 'string',
  40. 'regex:/^[0-9]{8}$/',
  41. ];
  42. $rules['address'] = 'required|string';
  43. $rules['address_type'] = 'required|in:home,commercial,other';
  44. }
  45. return $rules;
  46. }
  47. public function messages(): array
  48. {
  49. return [
  50. 'zip_code.regex' => __('validation.custom.zip_code.invalid'),
  51. 'source.in' => __('validation.custom.source.invalid'),
  52. 'address_type.in' => __('validation.custom.address_type.invalid'),
  53. ];
  54. }
  55. }