You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
4.2 KiB
103 lines
4.2 KiB
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Blacklist;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class BlacklistRequest extends FormRequest
|
|
{
|
|
/**
|
|
* 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.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'name' => 'nullable|string|max:100',
|
|
'mobile' => 'required|regex:/^1[3-9]\d{9}$/',
|
|
'credent' => 'required|in:1,2',
|
|
'idcard' => 'nullable|string|max:20',
|
|
'start_date' => 'nullable|date',
|
|
'end_date' => 'nullable|date|after_or_equal:start_date',
|
|
'status' => 'required|in:0,1',
|
|
'remark' => 'nullable|string|max:500',
|
|
'company_name' => 'nullable|string|max:200',
|
|
'file' => 'nullable|array',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'name.max' => __('validation.max.string', ['attribute' => __('blacklist.name'), 'max' => 100]),
|
|
'mobile.required' => __('validation.required', ['attribute' => __('blacklist.mobile')]),
|
|
'mobile.regex' => __('validation.regex', ['attribute' => __('blacklist.mobile')]),
|
|
'credent.required' => __('validation.required', ['attribute' => __('blacklist.credent')]),
|
|
'credent.in' => __('validation.in', ['attribute' => __('blacklist.credent')]),
|
|
'idcard.max' => __('validation.max.string', ['attribute' => __('blacklist.idcard'), 'max' => 20]),
|
|
'start_date.date' => __('validation.date', ['attribute' => __('blacklist.start_date')]),
|
|
'end_date.date' => __('validation.date', ['attribute' => __('blacklist.end_date')]),
|
|
'end_date.after_or_equal' => __('validation.after_or_equal', ['attribute' => __('blacklist.end_date'), 'date' => __('blacklist.start_date')]),
|
|
'status.required' => __('validation.required', ['attribute' => __('blacklist.status')]),
|
|
'status.in' => __('validation.in', ['attribute' => __('blacklist.status')]),
|
|
'remark.max' => __('validation.max.string', ['attribute' => __('blacklist.remark'), 'max' => 500]),
|
|
'company_name.max' => __('validation.max.string', ['attribute' => __('blacklist.company_name'), 'max' => 200]),
|
|
'file.array' => __('validation.array', ['attribute' => __('blacklist.file')]),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Configure the validator instance.
|
|
*/
|
|
public function withValidator($validator): void
|
|
{
|
|
$validator->after(function ($validator) {
|
|
// 检查手机号和证件号不能同时为空
|
|
if (empty($this->input('mobile')) && empty($this->input('idcard'))) {
|
|
$validator->errors()->add('mobile', __('blacklist.mobile_or_idcard_required'));
|
|
}
|
|
|
|
// 验证身份证号格式
|
|
if (!empty($this->input('idcard')) && $this->input('credent') == Blacklist::CREDENT_ID_CARD) {
|
|
$idcard = $this->input('idcard');
|
|
if (!preg_match('/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/', $idcard)) {
|
|
$validator->errors()->add('idcard', __('blacklist.invalid_idcard_format'));
|
|
}
|
|
}
|
|
|
|
// 检查是否已存在相同的黑名单记录
|
|
if ($this->has('mobile') || $this->has('idcard')) {
|
|
$query = Blacklist::query();
|
|
|
|
if ($this->has('mobile') && !empty($this->input('mobile'))) {
|
|
$query->where('mobile', $this->input('mobile'));
|
|
}
|
|
|
|
if ($this->has('idcard') && !empty($this->input('idcard'))) {
|
|
$query->where('idcard', $this->input('idcard'));
|
|
}
|
|
|
|
// 排除当前记录(更新时)
|
|
if ($this->has('id')) {
|
|
$query->where('id', '!=', $this->input('id'));
|
|
}
|
|
|
|
if ($query->exists()) {
|
|
$validator->errors()->add('mobile', __('blacklist.already_exists'));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|