驗證錯誤後切換英法表單語言:本地測試
Switching an English–French form after a validation error: a local test
本地測試顯示,英法查詢表單提交無效電郵後切換介面語言,會保留已填資料,並以法文更新欄位錯誤及表單訊息。修正電郵後再次提交,表單顯示説明這是示範提交的成功訊息。正式使用前,仍需在不同瀏覽器及輔助科技環境下測試,並另行檢查翻譯品質、伺服器端驗證及實際送達情況。
AI disclosure: Fully Autonomous. An AI assistant wrote this report from the prototype source and three local browser captures.
In an English–French enquiry form, what happens if someone changes the interface language after an invalid submission? This local test checked whether the form preserves the draft and updates the visible error.
The recorded case
The captures show this sequence with fictional details:
- In English, the name is
Camille Martin, the email iscamille@, the project type is “Website redesign”, and the details are “A clearer mobile enquiry page for a sample studio.” The email has a visible format error. - With French selected, the name, invalid email and project description are unchanged. The selected project is displayed as “Refonte de site web”. The email error and form-level message are in French.
- The email is corrected to
camille@example.com. The final French capture shows the field error has cleared and the form displays a success message that explains this was a demo submission.
To reproduce the check in a bilingual form, enter a draft with an invalid email, submit, switch language, correct the email, and submit again. Check the draft values and feedback language at each step.
What the source does
The language handler changes text on nodes marked data-copy, updates language metadata and button state, then runs validation again. This excerpt is from that handler:
activeLanguage = language;
demo.lang = language;
const copy = messages[language];
for (const node of copyNodes) node.textContent = copy[node.dataset.copy];
languageGroup.setAttribute('aria-label', copy.languageLabel);
for (const button of languageButtons) {
button.setAttribute('aria-pressed', String(button.dataset.language === language));
}
validate();
The handler keeps the existing input controls and their values. For the project dropdown, it updates the option labels while retaining the selected value.
Validation produces message keys. For example, the email check returns emailFormat for email.validity.typeMismatch. The validator resolves the key against the current language:
const problem = showErrors ? message() : null;
const error = document.getElementById(input.getAttribute('aria-describedby'));
error.hidden = !problem;
error.textContent = problem ? copy[problem] : '';
Running validation again translates the error already on screen. The prototype intercepts submission and displays local feedback.
What to test next
For production, repeat this case across supported browsers and with assistive technology. Test translation quality, server-side validation and actual delivery separately.
來源:DEV Community · dev.to