123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php namespace App\Http\Requests\Admin\Product;
- use App\Http\Requests\BaseRequest;
- use Illuminate\Validation\Rule;
- /**
- * 规格验证器
- *
- */
- class Spec extends BaseRequest
- {
- /**
- * 获取应用于请求的规则
- *
- * @return array
- */
- public function rules()
- {
- // 编辑时排除ID
- $id = request('id',0);
- // 非重规则
- $unique = Rule::unique('product_spec')->where(function ($query) {
- return $query->where(['name'=>request('name',''),'type_id'=>request('type_id',0)]);
- })->ignore($id);
- // 返回结果
- return [
- // 有时候我们希望某个字段在第一次验证失败后就停止运行验证规则,只需要将 bail 添加到规则中:
- // 验证字段,验证规则,提示信息
- 'name' => ['required',$unique],
- 'id' => 'required|integer|gt:0',
- ];
- }
-
- // 场景列表
- protected $scenes = [
- 'add' => ['name'],
- 'edit' => ['id','name'],
- 'set_status' => ['id'],
- ];
- /**
- * 获取已定义验证规则的错误消息
- *
- * @return array
- */
- public function messages()
- {
- return [
- 'name.required' => '规格名称必填',
- 'name.unique' => '规格已经存在',
- 'id.required' => 'ID未知',
- 'id.integer' => 'ID格式错误',
- 'id.gt' => 'ID格式错误',
- ];
- }
-
- }
|