AppUI 範例:將無效輸入與成功的介面結果分開
Keep invalid input separate from a successful UI result
AppUI 範例會在欄位內容變更時將舊結果標記為過時,並將錯誤訊息與成功的轉換結果分開顯示。輸入規則接受非負整數分鐘,最多 10080 分鐘;空白輸入、小數及負數均無效。互動測試亦確認修改輸入後舊結果會失效,並可在修正錯誤後重新轉換。
Posted on Nov 26 AI-assisted
A form can show a correct old result beside newly invalid input. That is a state bug even if the arithmetic is right. This AppUI example invalidates the previous result whenever the field changes and keeps error text separate from a successful conversion.
Run the example
I ran this form in PythonIDE on an English iPhone simulator.
import re
import appui
state = appui.State(minutes='', result='Enter whole minutes.', error='')
def parse_minutes(text):
if not re.fullmatch(r'[0-9]+', text):
raise ValueError('Use whole minutes, for example 95.')
value = int(text)
if value > 10080:
raise ValueError('Use at most 10080 minutes (one week).')
return divmod(value, 60)
def set_minutes(value):
state.minutes = value
state.result = 'Input changed; convert again.'
state.error = ''
def convert():
try:
hours, minutes = parse_minutes(state.minutes)
except ValueError as error:
state.error = str(error)
state.result = ''
return
state.error = ''
state.result = f'{hours} hours, {minutes} minutes'
assert parse_minutes('95') == (1, 35)
assert parse_minutes('0') == (0, 0)
def body():
return appui.NavigationStack(
appui.ScrollView([
appui.VStack([
appui.Text('Split a duration').font('title2').bold(),
appui.TextField('Whole minutes', text=state.minutes,
on_change=set_minutes),
appui.Button('Convert', action=convert)
.button_style('bordered_prominent'),
appui.Text(state.error or state.result).font('body'),
appui.Text('No saving, timer, or background work.')
.font('caption'),
], spacing=18, alignment='leading').padding(24)
]).navigation_title('Duration Form')
)
appui.run(body, state=state, presentation='fullscreen_with_close')
Read the behavior and its boundaries
The pure parse_minutes function owns the input contract: ASCII whole digits, non-negative, at most one week. It returns hours and remaining minutes using divmod. Keeping this rule outside body makes it testable without rendering a screen. Empty text and decimals are deliberately invalid, while zero is valid.
The named on_change callback updates state and marks the previous answer stale. Convert either clears the error and writes a new result or clears the result and displays a specific error. There is no saving, timer or background task. Editing and conversion callbacks were tested in the actual app.
The actual interaction checks included initial empty input, 95, 0, -1, 2.5, and 10081, plus editing after a success and recovering after a failure. A phone keyboard is a convenience, not validation: pasted text can bypass assumptions about available keys.
Observed run: 95 produced 1 hour and 35 minutes; 0 produced zero hours/minutes. Empty input, -1 and 2.5 produced the whole-minutes error; 10081 produced the one-week limit error. Editing marked the previous answer stale, and a valid input recovered after an error.
Try one change
Allow surrounding spaces by normalizing in the pure function. Add tests for " 95 " and an all-space value before changing the UI, then confirm stale results still clear on every edit.
I build PythonIDE, a Python coding app for iPhone and iPad. AI assisted with drafting and translation; the source and interaction results were checked in PythonIDE on an iPhone simulator.
來源:DEV Community · dev.to

