summaryrefslogtreecommitdiffstats
path: root/acf2/model/field.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-12-01 13:20:50 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-12-03 23:30:53 +0200
commitede05534efb0e4c373e661832de1a146482e1b3a (patch)
treed9ce213ec2f1d8f40ec35c6c7b26ee41e988f75c /acf2/model/field.lua
parent7fc2451896cdda1b44b3d2ef16c563a7a283b7bb (diff)
downloadaconf-ede05534efb0e4c373e661832de1a146482e1b3a.tar.bz2
aconf-ede05534efb0e4c373e661832de1a146482e1b3a.tar.xz
reference choice filters, disable choices for client
Diffstat (limited to 'acf2/model/field.lua')
-rw-r--r--acf2/model/field.lua45
1 files changed, 31 insertions, 14 deletions
diff --git a/acf2/model/field.lua b/acf2/model/field.lua
index d2470ca..e633a63 100644
--- a/acf2/model/field.lua
+++ b/acf2/model/field.lua
@@ -41,6 +41,16 @@ function M.Member:meta(context)
end
+function M.conv_filter(filter)
+ return filter and map(
+ function(values)
+ return type(values) == 'table' and values or {values}
+ end,
+ filter
+ ) or nil
+end
+
+
M.Field = class(M.Member)
function M.Field:init(params)
@@ -49,14 +59,7 @@ function M.Field:init(params)
if self.compute then self.addr = node.null_addr end
if self.editable == nil then self.editable = not self.compute end
- if self.condition then
- self.condition = map(
- function(values)
- return type(values) == 'table' and values or {values}
- end,
- self.condition
- )
- end
+ self.condition = M.conv_filter(self.condition)
if self.choice then
self.choice = map(
@@ -70,7 +73,8 @@ function M.Field:init(params)
end
end
return util.setdefaults(
- choice, {['ui-value']=self:auto_ui_name(choice.value)}
+ choice,
+ {enabled=true, ['ui-value']=self:auto_ui_name(choice.value)}
)
end,
self.choice
@@ -82,6 +86,8 @@ function M.Field:init(params)
end
end
+function M.Field:_choice(context) return self.choice end
+
function M.Field:meta(context)
assert(self.dtype)
local res = super(self, M.Field):meta(context)
@@ -91,7 +97,7 @@ function M.Field:meta(context)
res.condition = self.condition
res.required = self.required
res.default = self.default
- res.choice = self.choice
+ res.choice = self:_choice(context)
res.widget = self.widget
return res
@@ -118,14 +124,25 @@ function M.Field:_validate(context, value)
return
end
- value = self:normalize(context, value)
- if self.choice and not util.contains(
- map(function(ch) return ch.value end, self.choice), value
+ local save
+ value, save = self:normalize(context, value)
+
+ local committing = context.txn:committing()
+ local choice = self:_choice(context)
+ if choice and not util.contains(
+ map(
+ function(ch) return ch.value end,
+ util.filter(function(ch) return committing or ch.enabled end, choice)
+ ),
+ value
) then
raise(context.path, 'Invalid value')
end
+
self:validate(context, value)
- return value
+
+ if save == nil then save = value end
+ return save
end
function M.Field:normalize(context, value) return value end