Extending the Elm tutorial form app to include a numbered input Age

Any user-input from onInput event is a String.

Your Model expects it to be an Int

Use String.toInt to parse the integer value from a string value.

Adjust update function to convert the type to an Int and change the type signature to Age String

Age age ->
    case String.toInt age of
        Ok val ->
            { model | age = val }

        -- Notify the user, or simply ignore the value
        Err err ->
            model

That way you have an option to notify the user about the error.

In case if Maybe value suits you better, the whole statement can be simplified to:

Age age ->
    { model | age =  Result.toMaybe (String.toInt age) }

Tags:

Elm