CreateOrUpdateRequest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. 'custom_fields' => 'array',
  48. ];
  49. $customFieldsRules = $this->customFieldRuleByGroup("container");
  50. if ($this->has("naming_rule_id") && $this->get("naming_rule_id") > 0) {
  51. $this->namingRuleCheck($this->get("naming_rule_id"));
  52. $namingRules = $this->customFieldRuleByGroup($this->get("naming_rule_id"));
  53. $rules = [...$rules, ... $namingRules];
  54. }
  55. return [...$customFieldsRules, ...$rules];
  56. }
  57. }