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.
171 lines
6.0 KiB
171 lines
6.0 KiB
<?php
|
|
|
|
namespace App\Http\Requests\Admin;
|
|
|
|
use App\Models\Competition;
|
|
use App\Models\FormSchemaDefinition;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class UpdateCompetitionRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function (Validator $v): void {
|
|
$routeCompetition = $this->route('competition');
|
|
if (! $routeCompetition instanceof Competition) {
|
|
return;
|
|
}
|
|
|
|
$openInput = $this->input('signup_open_at');
|
|
$closeInput = $this->input('signup_close_at');
|
|
$open = $openInput ?? $routeCompetition->signup_open_at?->toIso8601String();
|
|
$close = $closeInput ?? $routeCompetition->signup_close_at?->toIso8601String();
|
|
|
|
if ($open && $close && strtotime((string) $close) < strtotime((string) $open)) {
|
|
$v->errors()->add(
|
|
'signup_close_at',
|
|
'报名截止时间须不早于开始时间。',
|
|
);
|
|
}
|
|
|
|
// 勿把 post_close 写成 settings.post_close_file_edit.* 规则:
|
|
// Laravel validated() 会只保留有嵌套规则的键,导致 success_notice / review_portal 被剥掉,
|
|
// 关闭补传时甚至整段 settings 从 validated 结果中消失、库内旧值无法覆盖。
|
|
$this->validatePostCloseFileEditSettings($v);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$competition = $this->route('competition');
|
|
assert($competition instanceof Competition);
|
|
|
|
return [
|
|
'slug' => [
|
|
'sometimes',
|
|
'string',
|
|
'max:128',
|
|
'regex:/^(?!.*\s)[\p{L}\p{N}\-_·]+$/u',
|
|
Rule::unique('competitions', 'slug')->ignore($competition->id),
|
|
],
|
|
'name' => ['sometimes', 'string', 'max:200'],
|
|
'description' => ['sometimes', 'nullable', 'string'],
|
|
'pledge_title' => ['sometimes', 'nullable', 'string', 'max:200'],
|
|
'pledge_title_en' => ['sometimes', 'nullable', 'string', 'max:200'],
|
|
'pledge_subtitle' => ['sometimes', 'nullable', 'string', 'max:500'],
|
|
'pledge_subtitle_en' => ['sometimes', 'nullable', 'string', 'max:500'],
|
|
'pledge_content_html' => ['sometimes', 'nullable', 'string', 'max:500000'],
|
|
'pledge_content_html_en' => ['sometimes', 'nullable', 'string', 'max:500000'],
|
|
'status' => ['sometimes', 'string', 'max:32', Rule::in(Competition::STATUSES)],
|
|
'published' => ['sometimes', 'boolean'],
|
|
'signup_open_at' => ['sometimes', 'nullable', 'date'],
|
|
'signup_close_at' => ['sometimes', 'nullable', 'date'],
|
|
'form_schema_id' => [
|
|
'sometimes',
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('form_schema_definitions', 'id')->where('purpose', FormSchemaDefinition::PURPOSE_SIGNUP),
|
|
],
|
|
'review_form_schema_id' => [
|
|
'sometimes',
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('form_schema_definitions', 'id')->where('purpose', FormSchemaDefinition::PURPOSE_REVIEW),
|
|
],
|
|
'audience_form_schema_id' => [
|
|
'sometimes',
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('form_schema_definitions', 'id')->where('purpose', FormSchemaDefinition::PURPOSE_AUDIENCE),
|
|
],
|
|
'branding_json' => ['sometimes', 'nullable', 'array'],
|
|
'settings' => ['sometimes', 'nullable', 'array'],
|
|
'scoring_rules_json' => ['sometimes', 'nullable', 'array'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'slug' => '访问地址',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'slug.regex' => ':attribute 仅可为中文、字母、数字、横线、下划线或中点,且不能含空格。',
|
|
];
|
|
}
|
|
|
|
private function validatePostCloseFileEditSettings(Validator $v): void
|
|
{
|
|
if (! $this->exists('settings')) {
|
|
return;
|
|
}
|
|
|
|
$settings = $this->input('settings');
|
|
if ($settings === null) {
|
|
return;
|
|
}
|
|
if (! is_array($settings)) {
|
|
$v->errors()->add('settings', '扩展设置格式无效');
|
|
|
|
return;
|
|
}
|
|
|
|
if (! array_key_exists('post_close_file_edit', $settings)) {
|
|
return;
|
|
}
|
|
|
|
$cfg = $settings['post_close_file_edit'];
|
|
if ($cfg === null) {
|
|
return;
|
|
}
|
|
if (! is_array($cfg)) {
|
|
$v->errors()->add('settings.post_close_file_edit', '截止后补传附件配置格式无效');
|
|
|
|
return;
|
|
}
|
|
|
|
$enabled = $cfg['enabled'] ?? false;
|
|
$isEnabled = $enabled === true || $enabled === 1 || $enabled === '1' || $enabled === 'true';
|
|
if (! $isEnabled) {
|
|
return;
|
|
}
|
|
|
|
$mobiles = $cfg['user_mobiles'] ?? null;
|
|
if (! is_array($mobiles) || count($mobiles) === 0) {
|
|
$v->errors()->add('settings.post_close_file_edit.user_mobiles', '开启截止后补传附件后,请至少填写一个登录手机号');
|
|
|
|
return;
|
|
}
|
|
|
|
foreach ($mobiles as $i => $mobile) {
|
|
$normalized = Competition::normalizeAllowlistMobile($mobile);
|
|
if ($normalized === '' || preg_match('/^1[3-9]\d{9}$/', $normalized) !== 1) {
|
|
$v->errors()->add(
|
|
"settings.post_close_file_edit.user_mobiles.{$i}",
|
|
'补传附件白名单手机号须为11位中国大陆手机号',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|