BatchCreateRequest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Http\Requests\API\User;
  3. use App\Http\Requests\RuleHelper;
  4. use App\Models\Company;
  5. use App\Models\Department;
  6. use App\Models\Role;
  7. use Illuminate\Foundation\Http\FormRequest;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Support\Facades\Hash;
  11. class BatchCreateRequest extends FormRequest
  12. {
  13. use RuleHelper;
  14. /**
  15. * Determine if the user is authorized to make this request.
  16. */
  17. public function authorize(): bool
  18. {
  19. return true;
  20. }
  21. /**
  22. * Get the validation rules that apply to the request.
  23. *
  24. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  25. */
  26. public function rules(): array
  27. {
  28. // $departmentIds=[];
  29. // if(Auth::user()->super_admin){ //若是超级管理员可以查看所有部门,且添加
  30. // $departmentIds = Department::withoutGlobalScopes()->pluck('id')->toArray();
  31. //
  32. // }else{
  33. // $departmentIds = Department::pluck('id')->toArray();
  34. // }
  35. // $roleIds = Role::pluck('id')->toArray();
  36. //
  37. // $IdsAndDitto=$departmentIds;
  38. // $IdsAndDitto[] = 'ditto';
  39. //
  40. // $IdsAndDittoWithRole=$roleIds;
  41. // $IdsAndDittoWithRole[]='ditto';
  42. return [
  43. 'users.*.name' => 'required|max:100',
  44. 'users.*.email'=> 'required|email|unique:users',
  45. 'users.*.username'=>'required|max:30|unique:users',
  46. // '*.pwd_is_ditto'=>'required|in:1,0',取消密码同上
  47. 'users.*.password' => 'required|min:6|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,}$/',//至少6位,包含大小写字母和数字,At least 6 digits, including upper and lower case letters and numbers
  48. 'users.*.gender'=>'nullable|in:1,0',
  49. 'users.*.company_id'=>'required|exists:company,id',
  50. 'users.*.department_id'=>'required|exists:department,id',
  51. 'users.*.role_id'=> 'required|exists:roles,id',
  52. 'auth_password' => [
  53. 'required',
  54. function ($attribute, $value, $fail) {
  55. if (!Hash::check($value, Auth::user()->password)) {
  56. $fail("Wrong security authentication password!");
  57. }
  58. }
  59. ],
  60. ];
  61. }
  62. }