CreateOrUpdateRequest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Requests\API\Container;
  3. use App\Http\Requests\CustomFieldRuleHelper;
  4. use App\Http\Requests\NamingRuleHelper;
  5. use App\Http\Requests\RuleHelper;
  6. use App\Models\Enums\ContainerACL;
  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. 'library_id' => [
  30. 'required',
  31. Rule::exists('libraries', 'id')->where($this->userCompanyWhere()),
  32. ],
  33. 'naming_rule_id' => [
  34. Rule::when($this->get('naming_rule_id') > 0, [
  35. Rule::exists('naming_rules', 'id')->whereIn('company_id', [
  36. 0, Auth::user()->company_id,
  37. ]),
  38. ])
  39. ],
  40. 'name' => 'required|max:150',
  41. 'email_subject' => 'max:255',
  42. 'acl' => [
  43. new Enum(ContainerACL::class),
  44. ],
  45. 'whitelist' => $this->usersCompanyRules(),
  46. 'mailto' => $this->usersCompanyRules(),
  47. ];
  48. $containerRules = $this->customFieldRuleByGroup("container", ['doc_type', 'doc_stage']);
  49. if ($this->has("naming_rule_id") && $this->get("naming_rule_id") > 0) {
  50. $this->namingRuleCheck($this->get("naming_rule_id"));
  51. $namingRules = $this->customFieldRuleByGroup($this->get("naming_rule_id"));
  52. $rules = [...$rules, ... $namingRules];
  53. }
  54. return [...$containerRules, ...$rules];
  55. }
  56. }