AddressRequest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 'has_complement' => 'sometimes|boolean',
  18. 'complement' => 'nullable|string|max:255',
  19. 'nickname' => 'nullable|string|max:255',
  20. 'instructions' => 'nullable|string',
  21. 'city_id' => 'nullable|integer|exists:cities,id',
  22. 'state_id' => 'nullable|integer|exists:states,id',
  23. 'address_type' => 'sometimes|in:home,commercial,other',
  24. ];
  25. if ($this->isMethod('post')) {
  26. $rules['source'] = 'required|string|in:provider,client';
  27. $rules['source_id'] = [
  28. 'required',
  29. 'integer',
  30. 'min:1',
  31. ];
  32. $rules['zip_code'] = [
  33. 'required',
  34. 'string',
  35. 'regex:/^[0-9]{8}$/',
  36. ];
  37. $rules['address'] = 'required|string';
  38. $rules['address_type'] = 'required|in:home,commercial,other';
  39. }
  40. return $rules;
  41. }
  42. public function messages(): array
  43. {
  44. return [
  45. 'zip_code.regex' => __('validation.custom.zip_code.invalid'),
  46. 'source.in' => __('validation.custom.source.invalid'),
  47. 'address_type.in' => __('validation.custom.address_type.invalid'),
  48. ];
  49. }
  50. }