DEV Community

Wenlo
Wenlo

Posted on AI-assisted

Keep invalid input separate from a successful UI result

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')
Enter fullscreen mode Exit fullscreen mode

PythonIDE editor showing the input validation and conversion callbacks

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.

Duration Form in the simulator showing 95 minutes converted to 1 hour and 35 minutes after correcting the input

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.

View on the App Store

Top comments (0)