CreateOrUpdateRequest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Http\Requests\API\Task;
  3. use App\Http\Requests\CustomFieldRuleHelper;
  4. use App\Http\Requests\NamingRuleHelper;
  5. use App\Http\Requests\RuleHelper;
  6. use App\Models\Enums\TaskACL;
  7. use Illuminate\Foundation\Http\FormRequest;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Validation\Rules\Enum;
  11. class CreateOrUpdateRequest extends FormRequest
  12. {
  13. use RuleHelper, CustomFieldRuleHelper, NamingRuleHelper;
  14. /**
  15. * Determine if the user is authorized to make this request.
  16. */
  17. public function authorize(): bool
  18. {
  19. return true;
  20. }
  21. /**
  22. * Get the validation rules that apply to the request.
  23. *
  24. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  25. */
  26. public function rules(): array
  27. {
  28. $rules = [
  29. 'project_id' => [
  30. 'required',
  31. Rule::exists('projects', 'id')->where($this->userCompanyWhere()),
  32. ],
  33. 'requirement_id' => [
  34. Rule::exists('requirements', 'id')->where($this->userCompanyWhere()),
  35. ],
  36. 'naming_rule_id' => [
  37. Rule::when($this->get('naming_rule_id') > 0, [
  38. Rule::exists('naming_rules', 'id')->whereIn('company_id', [
  39. 0, Auth::user()->company_id,
  40. ]),
  41. ])
  42. ],
  43. 'assign' => [
  44. Rule::exists('users', 'id')->where($this->userCompanyWhere()),
  45. ],
  46. 'name' => 'required|max:255',
  47. 'parent_id' => [
  48. Rule::when($this->get('parent_id') > 0, [
  49. Rule::exists('tasks', 'id')->where($this->userCompanyWhere())->where("parent_id", 0),
  50. ])
  51. ],
  52. 'begin' => 'date',
  53. 'end' => 'date',
  54. 'acl' => [
  55. new Enum(TaskACL::class),
  56. ],
  57. 'whitelist' => $this->usersCompanyRules(),
  58. 'mailto' => $this->usersCompanyRules(),
  59. ];
  60. $taskRules = $this->customFieldRuleByGroup("task", ['doc_type', 'task_type', 'doc_stage', "state", "suitability"]);
  61. if ($this->has("naming_rule_id") && $this->get("naming_rule_id") > 0) {
  62. $this->namingRuleCheck($this->get("naming_rule_id"));
  63. $namingRules = $this->customFieldRuleByGroup($this->get("naming_rule_id"));
  64. $rules = [...$rules, ... $namingRules];
  65. }
  66. return [...$rules, ...$taskRules];
  67. }
  68. }