module LoggedIn.Income.Model exposing ( Model , AddIncome , init , initForm ) import String exposing (toInt, split) import Date import Time exposing (Time) import Date.Extra.Create exposing (dateFromFields) import Date.Extra.Core exposing (intToMonth) import Form exposing (Form) import Form.Validate as Validate exposing (..) type alias Model = { addIncome : Form String AddIncome , test : Bool } type alias AddIncome = { time : Time , amount : Int } init : Model init = { addIncome = initForm , test = False } initForm : Form String AddIncome initForm = Form.initial [] validate validate : Validation String AddIncome validate = form2 AddIncome (get "creation" timeValidation) (get "amount" (int `andThen` (minInt 1))) timeValidation : Validation String Time timeValidation = customValidation string (\str -> case split "/" str of [day, month, year] -> case (toInt day, toInt month, toInt year) of (Ok dayNum, Ok monthNum, Ok yearNum) -> Ok (Date.toTime (dateFromFields yearNum (intToMonth monthNum) dayNum 0 0 0 0)) _ -> Err (customError "InvalidDate") _ -> Err (customError "InvalidDate") )