AddressRequest.php 1.8 KB

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