AddressRequest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. ];
  28. if ($this->isMethod('post')) {
  29. $rules['source'] = 'required|string|in:provider,client';
  30. $rules['source_id'] = [
  31. 'required',
  32. 'integer',
  33. 'min:1',
  34. ];
  35. $rules['zip_code'] = [
  36. 'required',
  37. 'string',
  38. 'regex:/^[0-9]{8}$/',
  39. ];
  40. $rules['address'] = 'required|string';
  41. $rules['address_type'] = 'required|in:home,commercial,other';
  42. }
  43. return $rules;
  44. }
  45. public function messages(): array
  46. {
  47. return [
  48. 'zip_code.regex' => __('validation.custom.zip_code.invalid'),
  49. 'source.in' => __('validation.custom.source.invalid'),
  50. 'address_type.in' => __('validation.custom.address_type.invalid'),
  51. ];
  52. }
  53. }