Hi,
this is my humble solution to this problem: https://runelm.io/c/a7u
In my application, I did not have to deal with numbers in forms, but I was
using "form" types like this:
type alias TextField = { value: String, error: String }
The validation was performed on update and form error messages was looking
for error not equal to empty string.
So, the idea with number fields:
type alias FloatField = { input: String, value: Maybe Float }
is like a variation of that one (you could add error here as well).
Regards,
Witold Szczerba
On Sun, Jan 22, 2017 at 9:36 AM, Simon <[email protected]> wrote:
> Hi,
> your idea looked interesting until I tried it. https://runelm.io/c/y3g
> Try entering a number and then backspacing to delete it. I could not get
> rid of the first digit
>
>
> I thought onChange would do it, but my early tests seem to suggest that it
> is firing just as often as onInput, which defeats the object
>
>
> On Sunday, 22 January 2017 02:11:56 UTC+1, j weir wrote:
>>
>> Even simpler is to just use input [] [type_ "number"] any reasons not to?
>>
>> http://caniuse.com/#feat=input-number
>>
>> https://gist.github.com/jweir/9e8412a4fa0132866977626e337cb164
>>
>> On Saturday, January 21, 2017 at 1:37:16 PM UTC-8, j weir wrote:
>>>
>>> You could also format the input so it is always a decimal.
>>>
>>> And instead of using Result.withDefault 0.0, revert to the existing
>>> value.
>>> This way the input won't be destroyed if the user fat fingers a non
>>> numeral.
>>>
>>> https://gist.github.com/jweir/9e8412a4fa0132866977626e337cb164
>>>
>>>
>>>
>>> On Saturday, January 21, 2017 at 9:11:41 AM UTC-8, Rafał Cieślak wrote:
>>>>
>>>> I'd consider storing the whole string in the model and passing it to
>>>> value. This way you can capture whatever input the user types and
>>>> convert it to float only when you actually need to do something with the
>>>> float.
>>>>
>>>> IMHO because the input tag allows you to type any kind of string, you
>>>> should treat the value that comes from its onBlur as a string. You
>>>> could set type_ to "number", but that would still not exclude some
>>>> invalid inputs from being provided.
>>>>
>>>> On Saturday, January 21, 2017 at 4:08:04 PM UTC+1, Simon wrote:
>>>>>
>>>>> I think it is not uncommon to collect dates and float values from
>>>>> users.
>>>>>
>>>>> update ...
>>>>> NewValue v ->
>>>>> { model | floatvalue = String.toFloat v |> Result.withDefault 0.0
>>>>> }
>>>>>
>>>>> floatInput : Float -> Html Msg
>>>>> floatInput v =
>>>>> input
>>>>> [ onInput NewValue
>>>>> , value (toString v)
>>>>> ] []
>>>>>
>>>>> The problem with the above is that the moment you type the . toFloat
>>>>> fails and you get a 0 in your model. One way around it could be to
>>>>> delay processing of the value by using onBlur (below), but I was wondering
>>>>> how others handled this.
>>>>>
>>>>> floatInput_ : Float -> Html Msg
>>>>> floatInput_ v =
>>>>> input
>>>>> [ onBlur NewValue
>>>>> , value (toString v)
>>>>> ] []
>>>>>
>>>>>
>>>>>
>>>> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.