CreateOrUpdateRequest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 'nullable',
  35. Rule::exists('requirements', 'id')->where($this->userCompanyWhere()),
  36. ],
  37. 'naming_rule_id' => [
  38. Rule::when($this->get('naming_rule_id') > 0, [
  39. Rule::exists('naming_rules', 'id')->whereIn('company_id', [
  40. 0, Auth::user()->company_id,
  41. ]),
  42. ])
  43. ],
  44. 'assign' => [
  45. 'nullable',
  46. Rule::exists('users', 'id')->where($this->userCompanyWhere()),
  47. ],
  48. 'name' => 'required|max:255',
  49. 'parent_id' => [
  50. Rule::when($this->get('parent_id') > 0, [
  51. Rule::exists('tasks', 'id')->where($this->userCompanyWhere())->where("parent_id", 0),
  52. ])
  53. ],
  54. 'begin' => 'date',
  55. 'end' => 'date',
  56. 'acl' => [
  57. new Enum(TaskACL::class),
  58. ],
  59. 'whitelist' => [
  60. 'array',
  61. 'nullable',
  62. function ($attribute, $value, $fail) {
  63. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  64. if ($userCount != count($value)) {
  65. $fail('The selected user is invalid.');
  66. }
  67. }
  68. ],
  69. 'mailto' => [
  70. 'array',
  71. 'nullable',
  72. function ($attribute, $value, $fail) {
  73. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  74. if ($userCount != count($value)) {
  75. $fail('The selected user is invalid.');
  76. }
  77. }
  78. ],
  79. ];
  80. $taskRules = $this->customFieldRuleByGroup("task", ['doc_type', 'task_type', 'doc_stage', "state", "suitability"]);
  81. if ($this->has("naming_rule_id") && $this->get("naming_rule_id") > 0) {
  82. $this->namingRuleCheck($this->get("naming_rule_id"));
  83. $namingRules = $this->customFieldRuleByGroup($this->get("naming_rule_id"));
  84. $rules = [...$rules, ... $namingRules];
  85. }
  86. return [...$rules, ...$taskRules];
  87. }
  88. }