added errors.nim module for UI error handling
This commit is contained in:
parent
0ddf6098f7
commit
a0665bc544
1 changed files with 30 additions and 0 deletions
30
karax/errors.nim
Normal file
30
karax/errors.nim
Normal 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, "")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue