PartnerAgreementRequest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. if ($this->isMethod('put')) {
  13. $partnerId = (int) $this->route('id') ?: PartnerAgreement::where('user_id', auth()->id())->value('id');
  14. if ($partnerId) {
  15. $cnpjUnique = 'unique:partner_agreements,cnpj,' . $partnerId;
  16. }
  17. }
  18. $rules = [
  19. 'user_name' => 'sometimes|nullable|string|max:255',
  20. 'user_email' => 'sometimes|nullable|email|max:255',
  21. 'user_password' => 'sometimes|nullable|string|min:6',
  22. 'company_name' => 'sometimes|string|max:255',
  23. 'cnpj' => ['sometimes', 'nullable', 'string', 'max:18', $cnpjUnique],
  24. 'category_id' => 'sometimes|nullable|integer|exists:categories,id',
  25. 'responsible' => 'sometimes|nullable|string|max:255',
  26. 'discount_percentage' => 'sometimes|nullable|numeric|min:0|max:100',
  27. 'description' => 'sometimes|nullable|string',
  28. 'email' => 'sometimes|nullable|email|max:255',
  29. 'phone' => 'sometimes|nullable|string|max:20',
  30. 'whatsapp' => 'sometimes|nullable|string|max:20',
  31. 'website' => 'sometimes|nullable|string|max:255',
  32. 'address' => 'sometimes|nullable|string|max:255',
  33. 'neighborhood' => 'sometimes|nullable|string|max:255',
  34. 'city_id' => 'sometimes|nullable|integer|exists:cities,id',
  35. 'state_id' => 'sometimes|nullable|integer|exists:states,id',
  36. 'zip_code' => 'sometimes|nullable|string|max:9',
  37. 'working_hours' => 'sometimes|nullable|string|max:255',
  38. 'contract_start' => 'sometimes|nullable|date',
  39. 'contract_end' => 'sometimes|nullable|date|after_or_equal:contract_start',
  40. 'status' => ['sometimes', Rule::enum(PartnerAgreementStatusEnum::class)],
  41. ];
  42. if ($this->isMethod('post')) {
  43. $rules['company_name'] = 'required|string|max:255';
  44. $rules['user_name'] = 'required|string|max:255';
  45. $rules['user_email'] = 'required|email|max:255|unique:users,email';
  46. $rules['user_password'] = 'required|string|min:6';
  47. }
  48. return $rules;
  49. }
  50. }