12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace App\Http\Requests\API\User;
- use App\Http\Requests\RuleHelper;
- use App\Models\Company;
- use App\Models\Department;
- use App\Models\Role;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Validation\Rule;
- class BatchCreateRequest extends FormRequest
- {
- use RuleHelper;
- /**
- * Determine if the user is authorized to make this request.
- */
- public function authorize(): bool
- {
- return true;
- }
- /**
- * Get the validation rules that apply to the request.
- *
- * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
- */
- public function rules(): array
- {
- $departmentIds=[];
- if(Auth::user()->super_admin){ //若是超级管理员可以查看所有部门,且添加
- $departmentIds = Department::withoutGlobalScopes()->pluck('id')->toArray();
- }else{
- $departmentIds = Department::pluck('id')->toArray();
- }
- $roleIds = Role::pluck('id')->toArray();
- $IdsAndDitto=$departmentIds;
- $IdsAndDitto[] = 'ditto';
- $IdsAndDittoWithRole=$roleIds;
- $IdsAndDittoWithRole[]='ditto';
- return [
- '*.name' => 'required|max:100',
- '*.email'=> 'required|email|unique:users',
- '*.username'=>'required|max:30|unique:users',
- // '*.pwd_is_ditto'=>'required|in:1,0',取消密码同上
- '*.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
- '*.gender'=>'nullable|in:1,0',
- '*.company_id'=>'required|exists:company,id',
- '*.department_id'=>[
- Rule::in($IdsAndDitto),
- ],
- '*.role_id'=>[
- Rule::in($IdsAndDittoWithRole),
- ],
- ];
- }
- }
|