summaryrefslogtreecommitdiffstats
path: root/acf2/model/field.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acf2/model/field.lua')
-rw-r--r--acf2/model/field.lua53
1 files changed, 39 insertions, 14 deletions
diff --git a/acf2/model/field.lua b/acf2/model/field.lua
index dbd98eb..69f36c6 100644
--- a/acf2/model/field.lua
+++ b/acf2/model/field.lua
@@ -87,7 +87,11 @@ function M.Field:init(params)
end
return setdefaults(
choice,
- {enabled=true, ['ui-value']=self:auto_ui_name(choice.value)}
+ {
+ be_value=choice.value,
+ enabled=true,
+ ['ui-value']=self:auto_ui_name(choice.value)
+ }
)
end,
self.choice
@@ -117,6 +121,7 @@ function M.Field:_choice(context) return self.choice end
function M.Field:meta(context)
assert(self.dtype)
+ local choice = self:_choice(context)
return util.update(
super(self, M.Field):meta(context),
{
@@ -126,7 +131,14 @@ function M.Field:meta(context)
condition=self.condition,
required=self.required,
default=self.default,
- choice=self:_choice(context),
+ choice=choice and map(
+ function(ch)
+ ch = util.copy(ch)
+ ch.be_value = nil
+ return ch
+ end,
+ choice
+ ),
widget=self.widget
}
)
@@ -158,25 +170,25 @@ function M.Field:_validate(context, value)
return
end
- local save
- value, save = self:normalize(context, value)
+ value = 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')
+ local be_value
+ if choice then
+ for _, ch in ipairs(choice) do
+ if ch.value == value and (committing or ch.enabled) then
+ be_value = ch.be_value
+ break
+ end
+ end
+ if be_value == nil then raise(context.path, 'Invalid value') end
end
self:validate(context, value)
- if save == nil then save = value end
- return save
+ if choice then return be_value end
+ return value
end
function M.Field:check_editable(context)
@@ -208,6 +220,19 @@ end
local Primitive = class(M.Field)
+function Primitive:_load(context)
+ local value = super(self, Primitive):_load(context)
+ if value == nil then return end
+
+ local choice = self:_choice(context)
+ if not choice then return value end
+
+ for _, ch in ipairs(choice) do
+ if ch.be_value == value then return ch.value end
+ end
+ assert(false)
+end
+
function Primitive:validate(context, value)
local t = self.dtype
if type(value) ~= t then raise(context.path, 'Not a '..t) end