ConfigRequest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Models\Config;
  4. use App\Rules\ConfigOptions;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Support\Arr;
  7. class ConfigRequest extends FormRequest
  8. {
  9. /**
  10. * Get the validation rules that apply to the request.
  11. *
  12. * @return array
  13. */
  14. public function rules()
  15. {
  16. $configId = $this->route()->originalParameter('config');
  17. $rules = [
  18. 'type' => 'required|in:'.implode(',', array_keys(Config::$typeMap)),
  19. 'category_id' => 'required|exists:config_categories,id',
  20. 'name' => 'required|string|max:50|unique:configs,name,'.(int) $configId,
  21. 'slug' => 'required|string|max:50|unique:configs,slug,'.(int) $configId,
  22. 'desc' => 'nullable|string|max:255',
  23. 'options' => new ConfigOptions($this->input('type')),
  24. 'value' => 'nullable',
  25. 'validation_rules' => 'nullable|string|max:255',
  26. ];
  27. if ($this->isMethod('put')) {
  28. $rules = Arr::only($rules, $this->keys());
  29. }
  30. return $rules;
  31. }
  32. public function attributes()
  33. {
  34. return [
  35. 'type' => '输入类型',
  36. 'category_id' => '分类',
  37. 'name' => '名称',
  38. 'slug' => '标识',
  39. 'desc' => '描述',
  40. 'options' => '选项',
  41. 'value' => '值',
  42. 'validation_rules' => '验证规则',
  43. ];
  44. }
  45. }