UnitRequest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. class UnitRequest extends FormRequest
  5. {
  6. public function rules(): array
  7. {
  8. $isCreate = $this->isMethod('POST');
  9. $rules = [
  10. 'fantasy_name' => 'sometimes|required|string|max:255',
  11. 'social_reason' => 'sometimes|required|string|max:255',
  12. 'cnpj' => 'sometimes|required|string|max:20',
  13. 'state_registration' => 'sometimes|nullable|string|max:50',
  14. 'name_responsible' => 'sometimes|required|string|max:255',
  15. 'street' => 'sometimes|required|string|max:255',
  16. 'address_number' => 'sometimes|nullable|string|max:20',
  17. 'postal_code' => 'sometimes|required|string|max:9',
  18. 'neighborhood' => 'sometimes|required|string|max:255',
  19. 'complement' => 'sometimes|nullable|string|max:255',
  20. 'city_id' => 'sometimes|required|integer|exists:cities,id',
  21. 'state_id' => 'sometimes|required|integer|exists:states,id',
  22. 'email' => 'sometimes|required|email|max:255',
  23. 'secondary_email' => 'sometimes|nullable|email|max:255',
  24. 'phone_number' => 'sometimes|required|string|max:20',
  25. 'cell_number' => 'sometimes|nullable|string|max:20',
  26. 'avatar' => 'sometimes|nullable|image|max:2048',
  27. ];
  28. if ($isCreate) {
  29. $required = [
  30. 'fantasy_name',
  31. 'social_reason',
  32. 'cnpj',
  33. 'name_responsible',
  34. 'street',
  35. 'postal_code',
  36. 'neighborhood',
  37. 'city_id',
  38. 'state_id',
  39. 'email',
  40. 'phone_number',
  41. ];
  42. foreach ($required as $field) {
  43. $rules[$field] = str_replace('sometimes|required', 'required', $rules[$field]);
  44. }
  45. }
  46. return $rules;
  47. }
  48. }