Fix form validation for app settings page w/ buefy theme

This commit is contained in:
Lance Edgar 2022-07-01 12:00:06 -05:00
parent 11cda10ca5
commit 7e0e881017
2 changed files with 34 additions and 27 deletions

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2021 Lance Edgar
# Copyright © 2010-2022 Lance Edgar
#
# This file is part of Rattail.
#
@ -28,12 +28,12 @@ from __future__ import unicode_literals, absolute_import
import re
import json
import six
from rattail.db import model, api
from rattail.settings import Setting
from rattail.util import import_module_path
from rattail.config import parse_bool
import colander
from webhelpers2.html import tags
@ -171,14 +171,28 @@ class AppSettingsView(View):
'data_type': setting.data_type.__name__,
'choices': setting.choices,
'helptext': form.render_helptext(field.name) if form.has_helptext(field.name) else None,
'error': field.error,
'error': False, # nb. may set to True below
}
value = self.get_setting_value(setting)
if setting.data_type is bool:
value = parse_bool(value)
# we want the value from the form, i.e. in case of a POST
# request with validation errors. we also want to make
# sure value is JSON-compatible, but we must represent it
# as Python value here, and it will be JSON-encoded later.
value = form.get_vuejs_model_value(field)
value = json.loads(value)
s['value'] = value
# specify error / message if applicable
# TODO: not entirely clear to me why some field errors are
# represented differently? presumably it depends on
# whether Buefy is used by the theme.
if field.error:
s['error_messages'] = field.error_messages()
s['error'] = True
if isinstance(field.error, colander.Invalid):
s['error_messages'] = [field.errormsg]
else:
s['error_messages'] = field.error_messages()
grouped[setting.group].append(s)
data = []