From 1e8a4534d531f77999f3750fdad04b5cf38e11d6 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 7 Dec 2017 20:35:44 -0600 Subject: [PATCH] Various forms2 changes --- tailbone/forms2/core.py | 23 ++++++++++++++++++----- tailbone/templates/base.mako | 11 +++++++++++ tailbone/templates/forms2/deform.mako | 8 ++++++-- tailbone/templates/master/create.mako | 9 --------- tailbone/templates/master/edit.mako | 9 --------- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/tailbone/forms2/core.py b/tailbone/forms2/core.py index cee0d4d8..c17b1052 100644 --- a/tailbone/forms2/core.py +++ b/tailbone/forms2/core.py @@ -360,12 +360,12 @@ class Form(object): self.remove(*args) def make_schema(self): - if not self.model_class: - # TODO - raise NotImplementedError - if not self.schema: + if not self.model_class: + # TODO + raise NotImplementedError + mapper = orm.class_mapper(self.model_class) # first filter our "full" field list so we ignore certain ones. in @@ -438,6 +438,10 @@ class Form(object): def set_label(self, key, label): self.labels[key] = label + # update schema if necessary + if self.schema and key in self.schema: + self.schema[key].title = label + def get_label(self, key): return self.labels.get(key, prettify(key)) @@ -498,6 +502,10 @@ class Form(object): def set_widget(self, key, widget): self.widgets[key] = widget + # update schema if necessary + if self.schema and key in self.schema: + self.schema[key].widget = widget + def set_validator(self, key, validator): self.validators[key] = validator @@ -659,8 +667,13 @@ class Form(object): return getattr(record, field_name) def validate(self, *args, **kwargs): + raise_error = kwargs.pop('raise_error', True) form = self.make_deform_form() - return form.validate(*args, **kwargs) + try: + return form.validate(*args, **kwargs) + except deform.ValidationFailure: + if raise_error: + raise class FieldList(list): diff --git a/tailbone/templates/base.mako b/tailbone/templates/base.mako index 7c138f6d..37ab3dd5 100644 --- a/tailbone/templates/base.mako +++ b/tailbone/templates/base.mako @@ -127,6 +127,17 @@ ${self.extra_javascript()} ${self.core_styles()} ${self.extra_styles()} + + ## TODO: should this be elsewhere / more customizable? + % if dform is not Undefined: + <% resources = dform.get_widget_resources() %> + % for path in resources['js']: + ${h.javascript_link(request.static_url(path))} + % endfor + % for path in resources['css']: + ${h.stylesheet_link(request.static_url(path))} + % endfor + % endif <%def name="core_javascript()"> diff --git a/tailbone/templates/forms2/deform.mako b/tailbone/templates/forms2/deform.mako index a8000189..3e42f9ba 100644 --- a/tailbone/templates/forms2/deform.mako +++ b/tailbone/templates/forms2/deform.mako @@ -2,7 +2,7 @@ % if not readonly: <% _focus_rendered = False %> -${h.form(form.action_url, id=dform.formid, method='post', enctype='multipart/form-data')} +${h.form(form.action_url, id=dform.formid, method='post', enctype='multipart/form-data', class_='autodisable')} ${h.csrf_token(request)} % endif @@ -26,7 +26,11 @@ ${h.csrf_token(request)} ##
${error}
## % endfor % if field.error: -
${field.error.msg}
+
+ % for msg in field.error.messages(): + ${msg} + % endfor +
% endif
diff --git a/tailbone/templates/master/create.mako b/tailbone/templates/master/create.mako index da86cb0b..53700f59 100644 --- a/tailbone/templates/master/create.mako +++ b/tailbone/templates/master/create.mako @@ -6,15 +6,6 @@ <%def name="extra_javascript()"> ${parent.extra_javascript()} ${self.disable_button_js()} - % if dform is not Undefined: - <% resources = dform.get_widget_resources() %> - % for path in resources['js']: - ${h.javascript_link(request.static_url(path))} - % endfor - % for path in resources['css']: - ${h.stylesheet_link(request.static_url(path))} - % endfor - % endif <%def name="disable_button_js()"> diff --git a/tailbone/templates/master/edit.mako b/tailbone/templates/master/edit.mako index 778ebd8d..6d4a9a60 100644 --- a/tailbone/templates/master/edit.mako +++ b/tailbone/templates/master/edit.mako @@ -18,15 +18,6 @@ }); - % if dform is not Undefined: - <% resources = dform.get_widget_resources() %> - % for path in resources['js']: - ${h.javascript_link(request.static_url(path))} - % endfor - % for path in resources['css']: - ${h.stylesheet_link(request.static_url(path))} - % endfor - % endif <%def name="context_menu_items()">