PartnerAgreementRequest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Enums\PartnerAgreementStatusEnum;
  4. use App\Models\PartnerAgreement;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Validation\Rule;
  7. class PartnerAgreementRequest extends FormRequest
  8. {
  9. public function rules(): array
  10. {
  11. $cnpjUnique = 'unique:partner_agreements,cnpj';
  12. $partnerId = PartnerAgreement::where('user_id', auth()->id())->value('id');
  13. if ($this->isMethod('put') && $partnerId) {
  14. $cnpjUnique = 'unique:partner_agreements,cnpj,' . $partnerId;
  15. }
  16. $rules = [
  17. 'user_name' => 'sometimes|nullable|string|max:255',
  18. 'user_email' => 'sometimes|nullable|email|max:255',
  19. 'user_password' => 'sometimes|nullable|string|min:6',
  20. 'company_name' => 'sometimes|string|max:255',
  21. 'cnpj' => ['sometimes', 'nullable', 'string', 'max:18', $cnpjUnique],
  22. 'category_id' => 'sometimes|nullable|integer|exists:categories,id',
  23. 'responsible' => 'sometimes|nullable|string|max:255',
  24. 'discount_percentage' => 'sometimes|nullable|numeric|min:0|max:100',
  25. 'description' => 'sometimes|nullable|string',
  26. 'email' => 'sometimes|nullable|email|max:255',
  27. 'phone' => 'sometimes|nullable|string|max:20',
  28. 'whatsapp' => 'sometimes|nullable|string|max:20',
  29. 'website' => 'sometimes|nullable|string|max:255',
  30. 'address' => 'sometimes|nullable|string|max:255',
  31. 'neighborhood' => 'sometimes|nullable|string|max:255',
  32. 'city_id' => 'sometimes|nullable|integer|exists:cities,id',
  33. 'state_id' => 'sometimes|nullable|integer|exists:states,id',
  34. 'zip_code' => 'sometimes|nullable|string|max:9',
  35. 'working_hours' => 'sometimes|nullable|string|max:255',
  36. 'contract_start' => 'sometimes|nullable|date',
  37. 'contract_end' => 'sometimes|nullable|date|after_or_equal:contract_start',
  38. 'status' => ['sometimes', Rule::enum(PartnerAgreementStatusEnum::class)],
  39. ];
  40. if ($this->isMethod('post')) {
  41. $rules['company_name'] = 'required|string|max:255';
  42. $rules['user_name'] = 'required|string|max:255';
  43. $rules['user_email'] = 'required|email|max:255|unique:users,email';
  44. $rules['user_password'] = 'required|string|min:6';
  45. }
  46. return $rules;
  47. }
  48. }