CreateOrUpdateRequest.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Requests\API\Plan;
  3. use App\Http\Requests\RuleHelper;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Validation\Rule;
  6. class CreateOrUpdateRequest extends FormRequest
  7. {
  8. use RuleHelper;
  9. /**
  10. * Determine if the user is authorized to make this request.
  11. */
  12. public function authorize(): bool
  13. {
  14. return true;
  15. }
  16. /**
  17. * Get the validation rules that apply to the request.
  18. *
  19. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  20. */
  21. public function rules(): array
  22. {
  23. return [
  24. 'asset_id' => [
  25. 'required',
  26. Rule::exists('assets', 'id')->where($this->userCompanyWhere()),
  27. ],
  28. 'title' => 'required|max:255',
  29. 'begin' => 'date',
  30. 'end' => 'date',
  31. 'parent_id' => [
  32. $this->parentIdExistsRule(),
  33. ]
  34. ];
  35. }
  36. protected function parentIdExistsRule()
  37. {
  38. // 如果 parent_id 不为 0,返回 exists 规则
  39. if ($this->input('parent_id') != 0) {
  40. return Rule::exists('plans', 'id')->where($this->userCompanyWhere())->where('asset_id', $this->input('asset_id'));
  41. }
  42. // 如果 parent_id 为 0,返回空数组以跳过 exists 验证
  43. return [];
  44. }
  45. }