12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace App\Http\Requests\API\RequirementGroup;
- use App\Http\Requests\RuleHelper;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Validation\Rule;
- /**
- */
- class CreateOrUpdateRequest extends FormRequest
- {
- use RuleHelper;
- /**
- * Determine if the user is authorized to make this request.
- */
- public function authorize(): bool
- {
- return true;
- }
- /**
- * Get the validation rules that apply to the request.
- *
- * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
- */
- public function rules(): array
- {
- return [
- 'asset_id' => [
- 'required',
- Rule::exists('assets', 'id')->where($this->userCompanyWhere()),
- ],
- 'name' => 'required|max:255',
- 'abbr_name' => 'required|max:30',
- 'parent_id' => [
- $this->parentIdExistsRule(),
- ]
- ];
- }
- protected function parentIdExistsRule()
- {
- // 如果 parent_id 不为 0,返回 exists 规则
- if ($this->input('parent_id') != 0) {
- return Rule::exists('requirement_groups', 'id')->where($this->userCompanyWhere())->where('asset_id', $this->input('asset_id'));
- }
- // 如果 parent_id 为 0,返回空数组以跳过 exists 验证
- return [];
- }
- }
|