176 lines
6.8 KiB
PHP
176 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Admin\Students;
|
|
|
|
use App\Http\Requests\BaseRequest;
|
|
use App\Models\Students\Student;
|
|
|
|
/**
|
|
* 学生请求验证类
|
|
*/
|
|
class StudentRequest extends BaseRequest
|
|
{
|
|
/**
|
|
* 获取特定的验证规则
|
|
* @return array
|
|
*/
|
|
protected function getSpecificRules(): array
|
|
{
|
|
$rules = [];
|
|
|
|
switch ($this->method()) {
|
|
case 'GET':
|
|
// 列表查询参数
|
|
$rules = [
|
|
'username' => 'nullable|string|max:200',
|
|
'real_name' => 'nullable|string|max:30',
|
|
'email' => 'nullable|string|max:200',
|
|
'phone_number' => 'nullable|string|max:11',
|
|
'role' => 'nullable|integer|in:2,3',
|
|
'status' => 'nullable|integer|in:0,1,2,3',
|
|
'sex' => 'nullable|integer|in:0,1,2',
|
|
'grade_id' => 'nullable|integer|min:0',
|
|
'parent_id' => 'nullable|integer|min:0',
|
|
'class_id' => 'nullable|integer|min:0',
|
|
];
|
|
break;
|
|
|
|
case 'POST':
|
|
// 创建学生
|
|
$rules = [
|
|
'username' => 'required|string|max:200|unique:student,username',
|
|
'real_name' => 'nullable|string|max:30',
|
|
'password' => 'required|string|min:6|max:20',
|
|
'role' => 'required|integer|in:2,3',
|
|
'grade_id' => 'nullable|integer|min:0',
|
|
'parent_id' => 'nullable|integer|exists:student,id',
|
|
'email' => 'nullable|email|max:200|unique:student,email',
|
|
'phone_number' => 'nullable|string|max:11|unique:student,phone_number',
|
|
'title' => 'nullable|string|max:200',
|
|
'avatar' => 'nullable|string|max:200',
|
|
'status' => 'nullable|integer|in:0,1,2,3',
|
|
'vip_level' => 'nullable|integer|min:0',
|
|
'sex' => 'nullable|integer|in:0,1,2',
|
|
'qq' => 'nullable|string|max:20',
|
|
'examing_number' => 'nullable|string|max:50',
|
|
];
|
|
break;
|
|
|
|
case 'PUT':
|
|
case 'PATCH':
|
|
// 更新学生
|
|
$studentId = $this->route('id');
|
|
$rules = [
|
|
'username' => 'nullable|string|max:200|unique:student,username,' . $studentId,
|
|
'real_name' => 'nullable|string|max:30',
|
|
'password' => 'nullable|string|min:6|max:20',
|
|
'role' => 'nullable|integer|in:2,3',
|
|
'grade_id' => 'nullable|integer|min:0',
|
|
'parent_id' => 'nullable|integer|exists:student,id',
|
|
'email' => 'nullable|email|max:200|unique:student,email,' . $studentId,
|
|
'phone_number' => 'nullable|string|max:11|unique:student,phone_number,' . $studentId,
|
|
'title' => 'nullable|string|max:200',
|
|
'avatar' => 'nullable|string|max:200',
|
|
'status' => 'nullable|integer|in:0,1,2,3',
|
|
'vip_level' => 'nullable|integer|min:0',
|
|
'sex' => 'nullable|integer|in:0,1,2',
|
|
'qq' => 'nullable|string|max:20',
|
|
'examing_number' => 'nullable|string|max:50',
|
|
];
|
|
break;
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* 获取特定的验证错误消息
|
|
* @return array
|
|
*/
|
|
protected function getSpecificMessages(): array
|
|
{
|
|
return [
|
|
'username.required' => '用户名不能为空',
|
|
'username.max' => '用户名最多200个字符',
|
|
'username.unique' => '用户名已存在',
|
|
'real_name.max' => '真实姓名最多30个字符',
|
|
'password.required' => '密码不能为空',
|
|
'password.min' => '密码长度至少6位',
|
|
'password.max' => '密码长度最多20位',
|
|
'role.required' => '角色不能为空',
|
|
'role.in' => '角色值无效',
|
|
'grade_id.integer' => '年级ID必须是整数',
|
|
'grade_id.min' => '年级ID不能小于0',
|
|
'parent_id.integer' => '家长ID必须是整数',
|
|
'parent_id.exists' => '家长不存在',
|
|
'email.email' => '邮箱格式不正确',
|
|
'email.max' => '邮箱最多200个字符',
|
|
'email.unique' => '邮箱已存在',
|
|
'phone_number.max' => '手机号最多11个字符',
|
|
'phone_number.unique' => '手机号已存在',
|
|
'title.max' => '头衔最多200个字符',
|
|
'avatar.max' => '头像URL最多200个字符',
|
|
'status.in' => '状态值无效',
|
|
'vip_level.integer' => 'VIP等级必须是整数',
|
|
'vip_level.min' => 'VIP等级不能小于0',
|
|
'sex.in' => '性别值无效',
|
|
'qq.max' => 'QQ号最多20个字符',
|
|
'examing_number.max' => '考号最多50个字符',
|
|
];
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 获取字段名称
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'username' => '用户名',
|
|
'real_name' => '真实姓名',
|
|
'password' => '密码',
|
|
'role' => '角色',
|
|
'grade_id' => '年级ID',
|
|
'parent_id' => '家长ID',
|
|
'email' => '邮箱',
|
|
'phone_number' => '手机号',
|
|
'title' => '头衔',
|
|
'avatar' => '头像',
|
|
'status' => '状态',
|
|
'vip_level' => 'VIP等级',
|
|
'sex' => '性别',
|
|
'qq' => 'QQ号',
|
|
'examing_number' => '考号',
|
|
'class_id' => '班级ID',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 配置验证器
|
|
* @param \Illuminate\Validation\Validator $validator
|
|
* @return void
|
|
*/
|
|
public function withValidator($validator): void
|
|
{
|
|
$validator->after(function ($validator) {
|
|
// 如果是创建学生并且指定了家长,验证家长必须是家长角色
|
|
if ($this->method() === 'POST' && $this->parent_id) {
|
|
$parent = Student::find($this->parent_id);
|
|
if ($parent && $parent->role !== Student::ROLE_PARENT) {
|
|
$validator->errors()->add('parent_id', '指定的家长用户角色不正确');
|
|
}
|
|
}
|
|
|
|
// 如果是学生角色,不能指定自己为家长
|
|
if ($this->role === Student::ROLE_STUDENT && $this->parent_id) {
|
|
$studentId = $this->method() === 'POST' ? 0 : $this->route('id');
|
|
if ($this->parent_id == $studentId) {
|
|
$validator->errors()->add('parent_id', '不能指定自己为家长');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|