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

@ -52,16 +52,12 @@
${h.csrf_token(request)} ${h.csrf_token(request)}
% if dform.error: % if dform.error:
<div class="error-messages"> <b-notification type="is-warning">
<div class="ui-state-error ui-corner-all"> Please see errors below.
<span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-alert"></span> </b-notification>
Please see errors below. <b-notification type="is-warning">
</div> ${dform.error}
<div class="ui-state-error ui-corner-all"> </b-notification>
<span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-alert"></span>
${dform.error}
</div>
</div>
% endif % endif
<div class="app-wrapper"> <div class="app-wrapper">
@ -115,17 +111,13 @@
## :class="'field-wrapper' + (setting.error ? ' with-error' : '')" ## :class="'field-wrapper' + (setting.error ? ' with-error' : '')"
> >
<div v-if="setting.error" class="field-error">
<span v-for="msg in setting.error_messages"
class="error-msg">
{{ msg }}
</span>
</div>
<div style="margin-bottom: 2rem;"> <div style="margin-bottom: 2rem;">
<b-field horizontal <b-field horizontal
:label="setting.label"> :label="setting.label"
:type="setting.error ? 'is-danger' : null"
## TODO: what if there are multiple error messages?
:message="setting.error ? setting.error_messages[0] : null">
<b-checkbox v-if="setting.data_type == 'bool'" <b-checkbox v-if="setting.data_type == 'bool'"
:name="setting.field_name" :name="setting.field_name"
@ -158,8 +150,9 @@
</b-field> </b-field>
<span v-if="setting.helptext" class="instructions"> <span v-if="setting.helptext"
{{ setting.helptext }} v-html="setting.helptext"
class="instructions">
</span> </span>
</div> </div>

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2021 Lance Edgar # Copyright © 2010-2022 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -28,12 +28,12 @@ from __future__ import unicode_literals, absolute_import
import re import re
import json
import six import six
from rattail.db import model, api from rattail.db import model, api
from rattail.settings import Setting from rattail.settings import Setting
from rattail.util import import_module_path from rattail.util import import_module_path
from rattail.config import parse_bool
import colander import colander
from webhelpers2.html import tags from webhelpers2.html import tags
@ -171,14 +171,28 @@ class AppSettingsView(View):
'data_type': setting.data_type.__name__, 'data_type': setting.data_type.__name__,
'choices': setting.choices, 'choices': setting.choices,
'helptext': form.render_helptext(field.name) if form.has_helptext(field.name) else None, '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: # we want the value from the form, i.e. in case of a POST
value = parse_bool(value) # 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 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: 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) grouped[setting.group].append(s)
data = [] data = []