Spec.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php namespace App\Http\Requests\Admin\Product;
  2. use App\Http\Requests\BaseRequest;
  3. use Illuminate\Validation\Rule;
  4. /**
  5. * 规格验证器
  6. *
  7. */
  8. class Spec extends BaseRequest
  9. {
  10. /**
  11. * 获取应用于请求的规则
  12. *
  13. * @return array
  14. */
  15. public function rules()
  16. {
  17. // 编辑时排除ID
  18. $id = request('id',0);
  19. // 非重规则
  20. $unique = Rule::unique('product_spec')->where(function ($query) {
  21. return $query->where(['name'=>request('name',''),'type_id'=>request('type_id',0)]);
  22. })->ignore($id);
  23. // 返回结果
  24. return [
  25. // 有时候我们希望某个字段在第一次验证失败后就停止运行验证规则,只需要将 bail 添加到规则中:
  26. // 验证字段,验证规则,提示信息
  27. 'name' => ['required',$unique],
  28. 'id' => 'required|integer|gt:0',
  29. ];
  30. }
  31. // 场景列表
  32. protected $scenes = [
  33. 'add' => ['name'],
  34. 'edit' => ['id','name'],
  35. 'set_status' => ['id'],
  36. ];
  37. /**
  38. * 获取已定义验证规则的错误消息
  39. *
  40. * @return array
  41. */
  42. public function messages()
  43. {
  44. return [
  45. 'name.required' => '规格名称必填',
  46. 'name.unique' => '规格已经存在',
  47. 'id.required' => 'ID未知',
  48. 'id.integer' => 'ID格式错误',
  49. 'id.gt' => 'ID格式错误',
  50. ];
  51. }
  52. }