HEL-364: pad typed dates and block invalid submit values

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
美术师
2026-09-01 23:29:45 +08:00
co-authored by Cursor multica-agent
parent 1b1f29e4ad
commit 82b16bb052
3 changed files with 145 additions and 38 deletions
-7
View File
@@ -43,13 +43,6 @@
$fs->execute([':a' => $editArchiveId]);
$editFiles = $fs->fetchAll();
}
$editArchiveId = (int)($editing['id'] ?? 0);
$editFiles = [];
if ($editArchiveId > 0) {
$fs = $pdo->prepare('SELECT * FROM files WHERE archive_id=:a ORDER BY id DESC');
$fs->execute([':a' => $editArchiveId]);
$editFiles = $fs->fetchAll();
}
pageHeader($me); ?>
<div class="crumbs"><button type="button" onclick="location.href='index.php'">‹ 档案库</button><span>/</span><span><?= $editing ? '编辑档案' : '新建档案' ?></span></div>
<div class="page-head">
+14 -16
View File
@@ -542,21 +542,7 @@ img { display: block; }
.v-fusion .timeline .plan-row[data-tl="due"]::before { border-color: var(--orange); }
.v-fusion .timeline .plan-row[data-tl="over"]::before { border-color: var(--red); }
/* 表单页:步骤条 + 右侧摘要 + 底部操作区 */
.v-fusion .steps { display: flex; align-items: center; gap: 0; margin-bottom: 22px; }
.v-fusion .step { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-3); white-space: nowrap; }
.v-fusion .step .no {
width: 24px; height: 24px; border-radius: 50%; flex: 0 0 auto;
display: flex; align-items: center; justify-content: center;
font-size: 12px; font-weight: 700;
background: var(--fill); color: var(--text-3);
transition: background-color 200ms ease, color 200ms ease;
}
.v-fusion .step[data-done] { color: var(--text-2); }
.v-fusion .step[data-done] .no { background: var(--green-bg); color: var(--green); }
.v-fusion .step[data-current] { color: var(--text); font-weight: 650; }
.v-fusion .step[data-current] .no { background: var(--accent-solid); color: #fff; }
.v-fusion .step-line { flex: 1; min-width: 18px; height: 1px; background: var(--hairline); margin: 0 12px; }
/* 表单页:右侧摘要 + 底部操作区 */
.v-fusion .editor-grid { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 18px; align-items: start; }
.v-fusion .editor-page { display: flex; flex-direction: column; gap: 18px; max-width: 880px; }
.v-fusion .editor-card { background: var(--surface); border: 1px solid var(--hairline); border-radius: 14px; padding: 20px 22px; }
@@ -906,7 +892,6 @@ img { display: block; }
.v-fusion .stat-value { font-size: 22px; }
.v-fusion .stat--gold .stat-value { font-size: 24px; }
.v-fusion .table-card { overflow-x: auto; }
.v-fusion .steps { overflow-x: auto; padding-bottom: 4px; }
.v-fusion .search { min-width: 0; flex: 1; }
.v-fusion .field-grid, .v-fusion .form-grid { grid-template-columns: 1fr; }
.v-fusion .summary-banner { grid-template-columns: 1fr; }
@@ -1011,6 +996,19 @@ img { display: block; }
.v-fusion input[data-jn-date]:disabled {
background-color: var(--fill); color: var(--text-3); cursor: not-allowed; opacity: 0.7;
}
.v-fusion input[type="date"][aria-invalid="true"],
.v-fusion input[data-jn-date][aria-invalid="true"],
.v-fusion input[type="date"][aria-invalid="true"]:focus,
.v-fusion input[type="date"][aria-invalid="true"]:focus-visible,
.v-fusion input[data-jn-date][aria-invalid="true"]:focus,
.v-fusion input[data-jn-date][aria-invalid="true"]:focus-visible,
.v-fusion .cr-ctl input[type="date"][aria-invalid="true"],
.v-fusion .cr-ctl input[data-jn-date][aria-invalid="true"],
.v-fusion .cr-ctl input[type="date"][aria-invalid="true"]:focus,
.v-fusion .cr-ctl input[data-jn-date][aria-invalid="true"]:focus {
border-color: var(--red);
box-shadow: 0 0 0 3px var(--red-glow);
}
.v-fusion input[type="date"]::-webkit-datetime-edit,
.v-fusion input[type="date"]::-webkit-datetime-edit-fields-wrapper,
.v-fusion input[type="date"]::-webkit-datetime-edit-text,
+131 -15
View File
@@ -107,17 +107,39 @@ function closeModal() { root()?.removeAttribute("data-modal-open"); }
/* ---------- 表单错误提示(与定版同结构) ---------- */
function setErr(input, msg) {
const field = input.closest(".form-field");
if (!field) return;
if (msg) {
field.setAttribute("data-error", "");
const e = field.querySelector("[data-err], .field-err");
if (e) e.textContent = msg;
input.setAttribute("aria-invalid", "true");
if (field) {
field.setAttribute("data-error", "");
let e = field.querySelector("[data-err], .field-err");
if (!e) {
e = document.createElement("div");
e.className = "field-err";
e.setAttribute("data-err", "");
field.appendChild(e);
}
e.textContent = msg;
} else {
let e = input.nextElementSibling;
if (!(e && e.hasAttribute("data-jn-date-err"))) {
e = document.createElement("div");
e.className = "field-err";
e.setAttribute("data-err", "");
e.setAttribute("data-jn-date-err", "");
input.insertAdjacentElement("afterend", e);
}
e.textContent = msg;
}
} else {
field.removeAttribute("data-error");
const e = field.querySelector("[data-err], .field-err");
if (e) e.textContent = "";
input.removeAttribute("aria-invalid");
if (field) {
field.removeAttribute("data-error");
const e = field.querySelector("[data-err], .field-err");
if (e) e.textContent = "";
} else {
const e = input.nextElementSibling;
if (e && e.hasAttribute("data-jn-date-err")) e.remove();
}
}
}
window.jnSetErr = setErr;
@@ -132,7 +154,19 @@ const CN_HOLIDAYS = (() => {
})();
const cPad = (n) => (n < 10 ? "0" : "") + n;
const cYmd = (d) => d.getFullYear() + "-" + cPad(d.getMonth() + 1) + "-" + cPad(d.getDate());
function cParseYmd(s) { const p = String(s || "").split("-"); if (p.length !== 3) return null; const d = new Date(+p[0], +p[1] - 1, +p[2]); return isNaN(d.getTime()) ? null : d; }
function ymdParts(s) {
const m = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(String(s || "").trim());
if (!m) return null;
const y = +m[1], mo = +m[2], d = +m[3];
if (!y || mo < 1 || mo > 12 || d < 1) return null;
const dt = new Date(y, mo - 1, d);
if (dt.getFullYear() !== y || dt.getMonth() !== mo - 1 || dt.getDate() !== d) return null;
return { y, mo, d, date: dt, ymd: y + "-" + cPad(mo) + "-" + cPad(d) };
}
function cParseYmd(s) {
const p = ymdParts(s);
return p ? p.date : null;
}
function cIsNonWork(d) { const w = d.getDay(); return w === 0 || w === 6 || !!CN_HOLIDAYS[cYmd(d)]; }
function cNextWorkday(ymdStr) {
const d = cParseYmd(ymdStr); if (!d) return ymdStr;
@@ -169,8 +203,9 @@ function caps(n) {
}
function ciDaysFromRange(d1, d2, mode) {
if (!d1 || !d2) return NaN;
const t0 = new Date(d1 + "T12:00:00").getTime(), t1 = new Date(d2 + "T12:00:00").getTime();
const a = ymdParts(d1), b = ymdParts(d2);
if (!a || !b) return NaN;
const t0 = new Date(a.ymd + "T12:00:00").getTime(), t1 = new Date(b.ymd + "T12:00:00").getTime();
if (t1 < t0) return NaN;
const diff = Math.round((t1 - t0) / 86400000);
return mode === "inclusive" ? diff + 1 : diff;
@@ -337,6 +372,7 @@ function syncCalcModeRows(target) {
}
function applyCdMonths() {
const r = root(); const f = r && $("[data-calc-discount]", r); if (!f) return;
if (f.issue && isDateField(f.issue) && !applyDateValue(f.issue).ok) return;
const iss = f.issue.value;
if (!iss) { toast("请先填出票日"); return; }
const mo = parseInt(f.months.value, 10);
@@ -357,12 +393,26 @@ function histRefill(i) {
function calcAction(act) {
const r = root(); if (!r) return;
const cd = $("[data-calc-discount]", r);
if (act === "calc") { initCalc(); return; }
if (act === "calc") {
const form = $("[data-calc-interest]", r) || $("[data-calc-discount]", r);
if (form && !normalizeFormDates(form)) return;
initCalc();
return;
}
if (act === "reset") {
const ci = $("[data-calc-interest]", r);
if (ci) { ci.amt.value = ""; ci.rate.value = ""; ci.days.value = ""; ci.d1.value = ""; ci.d2.value = ""; initCalc(); toast("已重置"); return; }
if (ci) {
ci.amt.value = ""; ci.rate.value = ""; ci.days.value = ""; ci.d1.value = ""; ci.d2.value = "";
[ci.d1, ci.d2].forEach((el) => { if (el) setErr(el, ""); });
initCalc(); toast("已重置"); return;
}
const cd2 = $("[data-calc-discount]", r);
if (cd2) { cd2.face.value = ""; cd2.rate.value = ""; cd2.issue.value = ""; cd2.due.value = ""; cd2.netIn.value = ""; cd2.per10.value = ""; const pv = $("[data-cd-mat-preview]", r); if (pv) pv.textContent = ""; initCalc(); toast("已重置"); return; }
if (cd2) {
cd2.face.value = ""; cd2.rate.value = ""; cd2.issue.value = ""; cd2.due.value = ""; cd2.netIn.value = ""; cd2.per10.value = "";
[cd2.issue, cd2.now, cd2.due].forEach((el) => { if (el) setErr(el, ""); });
const pv = $("[data-cd-mat-preview]", r); if (pv) pv.textContent = "";
initCalc(); toast("已重置"); return;
}
return;
}
if (act === "copy") {
@@ -553,6 +603,53 @@ function dateFieldMin(input) {
function dateFieldMax(input) {
return parseLimit(input.getAttribute("data-max") || input.max);
}
function applyDateValue(input, opts) {
const fire = !opts || opts.fire !== false;
const raw = String(input.value || "").trim();
if (raw === "") {
if (input.value !== "") input.value = "";
setErr(input, "");
return { ok: true, ymd: "" };
}
const parsed = ymdParts(raw);
if (!parsed) {
setErr(input, "请输入有效日期(YYYY-MM-DD)");
return { ok: false, ymd: "" };
}
const min = dateFieldMin(input), max = dateFieldMax(input);
const changed = input.value !== parsed.ymd;
if (changed) input.value = parsed.ymd;
if (!inDateRange(parsed.ymd, min, max)) {
if (changed && fire) {
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(new Event("change", { bubbles: true }));
}
setErr(input, "日期超出允许范围");
return { ok: false, ymd: parsed.ymd };
}
setErr(input, "");
if (changed && fire) {
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(new Event("change", { bubbles: true }));
}
return { ok: true, ymd: parsed.ymd };
}
function normalizeFormDates(form) {
if (!form) return true;
let ok = true, firstBad = null;
form.querySelectorAll('input[data-jn-date], input[type="date"]').forEach((input) => {
if (input.disabled) return;
const r = applyDateValue(input);
if (!r.ok) {
ok = false;
if (!firstBad) firstBad = input;
}
});
if (firstBad) {
try { firstBad.scrollIntoView({ block: "center", behavior: "smooth" }); firstBad.focus(); } catch (e) {}
}
return ok;
}
function yearBounds(input) {
const now = new Date().getFullYear();
let yMin = now - 40, yMax = now + 20;
@@ -607,7 +704,7 @@ function renderDatePicker() {
if (!dpEl || !popAnchor) return;
const input = popAnchor;
const min = dateFieldMin(input), max = dateFieldMax(input);
const selected = parseLimit(input.value);
const selected = (ymdParts(input.value) || {}).ymd || "";
const today = cYmd(new Date());
if (!dpView) {
const seed = cParseYmd(selected) || new Date();
@@ -714,6 +811,7 @@ function commitDate(ymd) {
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(new Event("change", { bubbles: true }));
}
setErr(input, "");
closePops();
input.focus();
return;
@@ -725,6 +823,7 @@ function commitDate(ymd) {
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(new Event("change", { bubbles: true }));
}
setErr(input, "");
closePops();
input.focus();
}
@@ -930,7 +1029,7 @@ document.addEventListener("change", (e) => {
});
document.addEventListener("input", (e) => {
if (e.target.closest(".form-field")) setErr(e.target, "");
if (e.target.closest(".form-field") || isDateField(e.target)) setErr(e.target, "");
if (e.target.closest("[data-calc-interest]") || e.target.closest("[data-calc-discount]")) {
const ci = e.target.closest("[data-calc-interest]");
if (ci) {
@@ -1084,6 +1183,23 @@ document.addEventListener("pointerdown", (e) => {
})
);
document.addEventListener("focusout", (e) => {
const input = e.target;
if (!isDateField(input) || input.disabled || input.readOnly) return;
const next = e.relatedTarget;
if (dpEl && next && dpEl.contains(next)) return;
applyDateValue(input);
});
document.addEventListener("submit", (e) => {
const form = e.target;
if (!form || form.tagName !== "FORM") return;
if (!normalizeFormDates(form)) {
e.preventDefault();
e.stopPropagation();
}
}, true);
/* ---------- 启动 ---------- */
document.addEventListener("DOMContentLoaded", () => {
syncThemeButtons();