added errors.nim module for UI error handling

This commit is contained in:
Andreas Rumpf 2017-11-13 15:27:58 +01:00
commit a0665bc544

30
karax/errors.nim Normal file
View file

@ -0,0 +1,30 @@
## Error handling logic for form input validation.
import karax, jdict
var
gerrorMsgs = newJDict[cstring, cstring]()
gerrorCounter = 0
proc hasErrors*(): bool = gerrorCounter != 0
proc disableOnError*(): cstring = toDisabled(hasErrors())
proc getError*(field: cstring): cstring =
if not gerrorMsgs.contains(field):
result = ""
else:
result = gerrorMsgs[field]
proc setError*(field, msg: cstring) =
let previous = getError(field)
if len(msg) == 0:
if len(previous) != 0: dec(gerrorCounter)
else:
if len(previous) == 0: inc(gerrorCounter)
gerrorMsgs[field] = msg
proc clearErrors*() =
let m = gerrorMsgs
for k in m.keys:
setError(k, "")