refactor(web/add_contest): uoj_form_v2

This commit is contained in:
Baoshuo Ren 2022-12-24 11:11:06 +08:00
parent 354dd361b0
commit f945a6cfdb
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -5,13 +5,13 @@ requirePHPLib('form');
Auth::check() || redirectToLogin(); Auth::check() || redirectToLogin();
UOJContest::userCanCreateContest(Auth::user()) || UOJResponse::page403(); UOJContest::userCanCreateContest(Auth::user()) || UOJResponse::page403();
$time_form = new UOJBs4Form('time'); $time_form = new UOJForm('time');
$time_form->addVInput( $time_form->addInput(
'name', 'name',
'text', [
'比赛标题', 'label' => '比赛标题',
'New Contest', 'default_value' => 'New Contest',
function ($name, &$vdata) { 'validator_php' => function ($name, &$vdata) {
if ($name == '') { if ($name == '') {
return '标题不能为空'; return '标题不能为空';
} }
@ -30,29 +30,32 @@ $time_form->addVInput(
return ''; return '';
}, },
null ]
); );
$time_form->addVInput( $time_form->addInput(
'start_time', 'start_time',
'text', [
'开始时间', 'div_class' => 'mt-2',
date("Y-m-d H:i:s"), 'label' => '开始时间',
function ($str, &$vdata) { 'default_value' => date("Y-m-d H:i:s"),
'validator_php' => function ($str, &$vdata) {
try { try {
$vdata['start_time'] = new DateTime($str); $vdata['start_time'] = new DateTime($str);
} catch (Exception $e) { } catch (Exception $e) {
return '无效时间格式'; return '无效时间格式';
} }
return ''; return '';
}, },
null ]
); );
$time_form->addVInput( $time_form->addInput(
'last_min', 'last_min',
'text', [
'时长(单位:分钟)', 'div_class' => 'mt-2',
180, 'label' => '时长(单位:分钟)',
function ($str, &$vdata) { 'default_value' => '180',
'validator_php' => function ($str, &$vdata) {
if (!validateUInt($str)) { if (!validateUInt($str)) {
return '必须为一个整数'; return '必须为一个整数';
} }
@ -61,7 +64,7 @@ $time_form->addVInput(
return ''; return '';
}, },
null ]
); );
$time_form->handle = function (&$vdata) { $time_form->handle = function (&$vdata) {
$start_time_str = $vdata['start_time']->format('Y-m-d H:i:s'); $start_time_str = $vdata['start_time']->format('Y-m-d H:i:s');
@ -75,6 +78,7 @@ $time_form->handle = function (&$vdata) {
$time_form->succ_href = "/contests"; $time_form->succ_href = "/contests";
$time_form->runAtServer(); $time_form->runAtServer();
?> ?>
<?php echoUOJPageHeader('添加比赛') ?> <?php echoUOJPageHeader('添加比赛') ?>
<div class="row"> <div class="row">
@ -82,7 +86,6 @@ $time_form->runAtServer();
<div class="col-lg-9"> <div class="col-lg-9">
<div class="card card-default mb-2"> <div class="card card-default mb-2">
<div class="card-body"> <div class="card-body">
<h1 class="card-title">添加比赛</h1> <h1 class="card-title">添加比赛</h1>
<div class="w-full" style="max-width: 400px"> <div class="w-full" style="max-width: 400px">