# HG changeset patch # User sylvain.thenault@logilab.fr # Date 1241626412 -7200 # Node ID 8c717cc0b353ac9de53ee88fc7c36a49ec44a3de # Parent f7110f533d14c07506faa1349bbf9c6884ac6f15 refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed diff -r f7110f533d14 -r 8c717cc0b353 web/form.py --- a/web/form.py Wed May 06 17:24:48 2009 +0200 +++ b/web/form.py Wed May 06 18:13:32 2009 +0200 @@ -75,18 +75,22 @@ # method on successful commit forminfo = self.req.get_session_data(sessionkey, pop=True) if forminfo: - self.req.data['formvalues'] = forminfo['values'] - self.req.data['formerrors'] = errex = forminfo['errors'] - self.req.data['displayederrors'] = set() + # XXX remove req.data assigment once cw.web.widget is killed + self.req.data['formvalues'] = self.form_previous_values = forminfo['values'] + self.req.data['formerrors'] = self.form_valerror = forminfo['errors'] + self.req.data['displayederrors'] = self.form_displayed_errors = set() # if some validation error occured on entity creation, we have to # get the original variable name from its attributed eid - foreid = errex.entity + foreid = self.form_valerror.entity for var, eid in forminfo['eidmap'].items(): if foreid == eid: - errex.eid = var + self.form_valerror.eid = var break else: - errex.eid = foreid + self.form_valerror.eid = foreid + else: + self.form_previous_values = {} + self.form_valerror = None # XXX deprecated with new form system. Should disappear @@ -166,11 +170,11 @@ This method should be called once inlined field errors has been consumed """ - errex = self.req.data.get('formerrors') + errex = self.req.data.get('formerrors') or self.form_valerror # get extra errors if errex is not None: errormsg = self.req._('please correct the following errors:') - displayed = self.req.data['displayederrors'] + displayed = self.req.data.get('displayederrors') or self.form_displayed_errors errors = sorted((field, err) for field, err in errex.errors.items() if not field in displayed) if errors: @@ -333,9 +337,6 @@ form_render() """ self.context = context = {} - # on validation error, we get a dictionary of previously submitted - # values - self._previous_values = self.req.data.get('formvalues', {}) # ensure rendervalues is a dict if rendervalues is None: rendervalues = {} @@ -361,8 +362,8 @@ value while those found in 3. and 4. are expected to be correctly typed. """ qname = self.form_field_name(field) - if qname in self._previous_values: - value = self._previous_values[qname] + if qname in self.form_previous_values: + value = self.form_previous_values[qname] elif qname in self.req.form: value = self.req.form[qname] else: @@ -385,10 +386,9 @@ def form_field_error(self, field): """return validation error for widget's field, if any""" - errex = self.req.data.get('formerrors') - if errex and self._errex_match_field(errex, field): - self.req.data['displayederrors'].add(field.name) - return u'%s' % errex.errors[field.name] + if self._field_has_error(field): + self.form_displayed_errors.add(field.name) + return u'%s' % self.form_valerror.errors[field.name] return u'' def form_field_format(self, field): @@ -413,10 +413,10 @@ """ raise NotImplementedError - def _errex_match_field(self, errex, field): + def _field_has_error(self, field): """return true if the field has some error in given validation exception """ - return field.name in errex.errors + return self.form_valerror and field.name in self.form_valerror.errors class EntityFieldsForm(FieldsForm): @@ -443,10 +443,11 @@ self.schema = self.edited_entity.schema self.vreg = self.edited_entity.vreg - def _errex_match_field(self, errex, field): + def _field_has_error(self, field): """return true if the field has some error in given validation exception """ - return errex.eid == self.edited_entity.eid and field.name in errex.errors + return super(EntityFieldsForm, self)._field_has_error(field) \ + and self.form_valerror.eid == self.edited_entity.eid def _relation_vocabulary(self, rtype, targettype, role, limit=None, done=None): diff -r f7110f533d14 -r 8c717cc0b353 web/formrenderers.py --- a/web/formrenderers.py Wed May 06 17:24:48 2009 +0200 +++ b/web/formrenderers.py Wed May 06 18:13:32 2009 +0200 @@ -31,11 +31,12 @@ +---------+ """ _options = ('display_fields', 'display_label', 'display_help', - 'display_progress_div', 'button_bar_class') + 'display_progress_div', 'table_class', 'button_bar_class') display_fields = None # None -> all fields display_label = True display_help = True display_progress_div = True + table_class = u'attributeForm' button_bar_class = u'formButtonBar' def __init__(self, **kwargs): @@ -100,11 +101,11 @@ This method should be called once inlined field errors has been consumed """ req = form.req - errex = req.data.get('formerrors') + errex = form.form_valerror # get extra errors if errex is not None: errormsg = req._('please correct the following errors:') - displayed = req.data['displayederrors'] + displayed = form.form_displayed_errors errors = sorted((field, err) for field, err in errex.errors.items() if not field in displayed) if errors: @@ -176,7 +177,7 @@ return fields def _render_fields(self, fields, w, form): - w(u'