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