CreateOrUpdateRequest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. 'required',
  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. Rule::exists('users', 'id')->where($this->userCompanyWhere()),
  46. ],
  47. 'name' => 'required|max:255',
  48. 'parent_id' => [
  49. Rule::when($this->get('parent_id') > 0, [
  50. Rule::exists('tasks', 'id')->where($this->userCompanyWhere())->where("parent_id", 0),
  51. ])
  52. ],
  53. 'begin' => 'date',
  54. 'end' => 'date',
  55. 'acl' => [
  56. new Enum(TaskACL::class),
  57. ],
  58. 'whitelist' => $this->usersCompanyRules(),
  59. 'mailto' => $this->usersCompanyRules(),
  60. ];
  61. $taskRules = $this->customFieldRuleByGroup("task", ['doc_type', 'task_type', 'doc_stage', "state", "suitability"]);
  62. if ($this->has("naming_rule_id") && $this->get("naming_rule_id") > 0) {
  63. $this->namingRuleCheck($this->get("naming_rule_id"));
  64. $namingRules = $this->customFieldRuleByGroup($this->get("naming_rule_id"));
  65. $rules = [...$rules, ... $namingRules];
  66. }
  67. return [...$rules, ...$taskRules];
  68. }
  69. }