diff --git a/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm b/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm index deb3dc81..b0ff5137 100644 --- a/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm +++ b/modules/webapp/src/main/elm/Comp/CustomFieldInput.elm @@ -131,14 +131,17 @@ initWith value = Data.CustomFieldType.Numeric -> let ( fm, _ ) = - updateFloatModel value.value string2Float + updateFloatModel value.value string2Float identity in NumberField fm Data.CustomFieldType.Money -> let ( fm, _ ) = - updateFloatModel value.value Data.Money.fromString + updateFloatModel + value.value + Data.Money.fromString + Data.Money.normalizeInput in MoneyField fm @@ -175,14 +178,18 @@ type alias UpdateResult = } -updateFloatModel : String -> (String -> Result String Float) -> ( FloatModel, FieldResult ) -updateFloatModel msg parse = +updateFloatModel : + String + -> (String -> Result String Float) + -> (String -> String) + -> ( FloatModel, FieldResult ) +updateFloatModel msg parse normalize = case parse msg of Ok n -> - ( { input = msg + ( { input = normalize msg , result = Ok n } - , Value msg + , Value (normalize msg) ) Err err -> @@ -216,7 +223,7 @@ update msg model = ( NumberMsg str, NumberField _ ) -> let ( fm, res ) = - updateFloatModel str string2Float + updateFloatModel str string2Float identity model_ = { model | fieldModel = NumberField fm } @@ -229,6 +236,7 @@ update msg model = updateFloatModel str Data.Money.fromString + Data.Money.normalizeInput model_ = { model | fieldModel = MoneyField fm } diff --git a/modules/webapp/src/main/elm/Data/Money.elm b/modules/webapp/src/main/elm/Data/Money.elm index c62c3144..7856f517 100644 --- a/modules/webapp/src/main/elm/Data/Money.elm +++ b/modules/webapp/src/main/elm/Data/Money.elm @@ -2,6 +2,7 @@ module Data.Money exposing ( Money , format , fromString + , normalizeInput , roundMoney ) @@ -13,8 +14,11 @@ type alias Money = fromString : String -> Result String Money fromString str = let + input = + normalizeInput str + points = - String.indexes "." str + String.indexes "." input len = String.length str @@ -22,7 +26,7 @@ fromString str = case points of index :: [] -> if index == (len - 3) then - String.toFloat str + String.toFloat input |> Maybe.map Ok |> Maybe.withDefault (Err "Two digits required after the dot.") @@ -41,3 +45,8 @@ format money = roundMoney : Float -> Float roundMoney input = (round (input * 100) |> toFloat) / 100 + + +normalizeInput : String -> String +normalizeInput str = + String.replace "," "." str