Commit e53fc10e authored by Jean-Paul Smets's avatar Jean-Paul Smets

Implementation of filed validation in Listbox


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@624 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 27097fee
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
from Products.Formulator.Form import Form, BasicForm, ZMIForm from Products.Formulator.Form import Form, BasicForm, ZMIForm
from Products.Formulator.Form import manage_addForm, manage_add, initializeForm from Products.Formulator.Form import manage_addForm, manage_add, initializeForm
from Products.Formulator.Errors import FormValidationError, ValidationError
from Products.Formulator.DummyField import fields from Products.Formulator.DummyField import fields
from Products.Formulator.XMLToForm import XMLToForm from Products.Formulator.XMLToForm import XMLToForm
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
...@@ -141,7 +142,7 @@ class ERP5Field(Field): ...@@ -141,7 +142,7 @@ class ERP5Field(Field):
'alt': self.meta_type, 'title': self.meta_type},) 'alt': self.meta_type, 'title': self.meta_type},)
return icons return icons
psyco.bind(get_value) psyco.bind(get_value)
# Dynamic Patch # Dynamic Patch
Field.get_value = ERP5Field.get_value Field.get_value = ERP5Field.get_value
...@@ -336,6 +337,36 @@ class ERP5Form(ZMIForm, ZopePageTemplate): ...@@ -336,6 +337,36 @@ class ERP5Form(ZMIForm, ZopePageTemplate):
'alt': self.meta_type, 'title': self.meta_type},) 'alt': self.meta_type, 'title': self.meta_type},)
return icons return icons
# Pached validate_all to support ListBox validation
security.declareProtected('View', 'validate_all')
def validate_all(self, REQUEST):
"""Validate all enabled fields in this form, catch any ValidationErrors
if they occur and raise a FormValidationError in the end if any
Validation Errors occured.
"""
result = {}
errors = []
for field in self.get_fields():
# skip any field we don't need to validate
if not field.need_validate(REQUEST):
continue
try:
value = field.validate(REQUEST)
# store under id
result[field.id] = value
# store as alternate name as well if necessary
alternate_name = field.get_value('alternate_name')
if alternate_name:
result[alternate_name] = value
except FormValidationError, e: # XXX JPS Patch for listbox
errors.extend(e.errors)
result.update(e.result)
except ValidationError, err:
errors.append(err)
if len(errors) > 0:
raise FormValidationError(errors, result)
return result
# FTP/DAV Access # FTP/DAV Access
manage_FTPget = ZMIForm.get_xml manage_FTPget = ZMIForm.get_xml
......
...@@ -31,6 +31,7 @@ from Products.Formulator.DummyField import fields ...@@ -31,6 +31,7 @@ from Products.Formulator.DummyField import fields
from Products.Formulator import Widget, Validator from Products.Formulator import Widget, Validator
from Products.Formulator.Field import ZMIField from Products.Formulator.Field import ZMIField
from Products.Formulator.Form import BasicForm from Products.Formulator.Form import BasicForm
from Products.Formulator.Errors import FormValidationError, ValidationError
from Products.Formulator.MethodField import BoundMethod from Products.Formulator.MethodField import BoundMethod
from Selection import Selection from Selection import Selection
from DateTime import DateTime from DateTime import DateTime
...@@ -41,7 +42,7 @@ from copy import copy ...@@ -41,7 +42,7 @@ from copy import copy
from Acquisition import aq_base, aq_inner, aq_parent, aq_self from Acquisition import aq_base, aq_inner, aq_parent, aq_self
from zLOG import LOG from zLOG import LOG
import random import random
import md5 import md5
def getAsList(a): def getAsList(a):
...@@ -311,6 +312,7 @@ class ListBoxWidget(Widget.Widget): ...@@ -311,6 +312,7 @@ class ListBoxWidget(Widget.Widget):
here = REQUEST['here'] here = REQUEST['here']
reset = REQUEST.get('reset', 0) reset = REQUEST.get('reset', 0)
form = field.aq_parent form = field.aq_parent
field_errors = REQUEST.get('field_errors',{});
field_title = field.get_value('title') field_title = field.get_value('title')
lines = field.get_value('lines') lines = field.get_value('lines')
meta_types = field.get_value('meta_types') meta_types = field.get_value('meta_types')
...@@ -559,11 +561,11 @@ class ListBoxWidget(Widget.Widget): ...@@ -559,11 +561,11 @@ class ListBoxWidget(Widget.Widget):
for (k,v) in columns: for (k,v) in columns:
try: try:
if stats[index] != ' ': if stats[index] != ' ':
stat_query += stats[index] + '(' + k + ') as ' + k + ',' stat_query += stats[index] + '(' + k + ') AS ' + k + ','
else: else:
stat_query += '\' \' as ' + k + ',' stat_query += '\' \' AS ' + k + ','
except: except:
stat_query += '\' \' as ' + k + ',' stat_query += '\' \' AS ' + k + ','
index = index + 1 index = index + 1
stat_query = stat_query[:len(stat_query) - 1] stat_query = stat_query[:len(stat_query) - 1]
...@@ -990,6 +992,7 @@ onChange="submitAction(this.form,'%s/portal_selections/setReportRoot')"> ...@@ -990,6 +992,7 @@ onChange="submitAction(this.form,'%s/portal_selections/setReportRoot')">
"""<td class="%s" width="50" align="center" valign="middle">&nbsp; """<td class="%s" width="50" align="center" valign="middle">&nbsp;
<input type="checkbox" %s value="%s" id="cb_%s" name="uids:list"/></td> <input type="checkbox" %s value="%s" id="cb_%s" name="uids:list"/></td>
""" % (td_css, selected, o.uid , o.uid) """ % (td_css, selected, o.uid , o.uid)
error_list = []
for cname in extended_columns: for cname in extended_columns:
sql = cname[0] # (sql, title, alias) sql = cname[0] # (sql, title, alias)
alias = cname[2] # (sql, title, alias) alias = cname[2] # (sql, title, alias)
...@@ -1045,10 +1048,21 @@ onChange="submitAction(this.form,'%s/portal_selections/setReportRoot')"> ...@@ -1045,10 +1048,21 @@ onChange="submitAction(this.form,'%s/portal_selections/setReportRoot')">
my_field_id = '%s_%s' % (field.id, alias) my_field_id = '%s_%s' % (field.id, alias)
my_field = form.get_field(my_field_id) my_field = form.get_field(my_field_id)
key = my_field.id + '_%s' % o.uid key = my_field.id + '_%s' % o.uid
#if attribute_value is Noe if field_errors.has_key(key):
cell_body = my_field.render(value = attribute_value, REQUEST = o, key = key) error_css = 'Error'
error_message = "<br/>%s" % field_errors[key].error_text # XXX localization needed
# Display previous value (in case of error
error_list.append(field_errors.get(key))
display_value = REQUEST.get('field_%s' % key, attribute_value)
else:
error_css = ''
error_message = ''
display_value = REQUEST.get(key, attribute_value)
cell_body = my_field.render(value = display_value, REQUEST = o, key = key)
# We use REQUEST which is not so good here
# This prevents from using standard display process
list_body = list_body + \ list_body = list_body + \
('<td class=\"%s\">%s</td>' % (td_css, cell_body)) ('<td class=\"%s%s\">%s%s</td>' % (td_css, error_css, cell_body, error_message))
else: else:
# Check if this object provides a specific URL method # Check if this object provides a specific URL method
url_method = getattr(o, 'getListItemUrl', None) url_method = getattr(o, 'getListItemUrl', None)
...@@ -1167,6 +1181,7 @@ class ListBoxValidator(Validator.Validator): ...@@ -1167,6 +1181,7 @@ class ListBoxValidator(Validator.Validator):
#LOG('In Listbox Validator',0,'') #LOG('In Listbox Validator',0,'')
result = {} result = {}
error_result = {}
listbox_uids = REQUEST.get('%s_uid' % field.id, []) listbox_uids = REQUEST.get('%s_uid' % field.id, [])
errors = [] errors = []
for uid in listbox_uids: for uid in listbox_uids:
...@@ -1184,44 +1199,49 @@ class ListBoxValidator(Validator.Validator): ...@@ -1184,44 +1199,49 @@ class ListBoxValidator(Validator.Validator):
if form.has_field( my_field_id ): if form.has_field( my_field_id ):
my_field = form.get_field(my_field_id) my_field = form.get_field(my_field_id)
key = 'field_' + my_field.id + '_%s' % o.uid key = 'field_' + my_field.id + '_%s' % o.uid
error_result_key = my_field.id + '_%s' % o.uid
#if hasattr(o,cname_id): WHY THIS ???? #if hasattr(o,cname_id): WHY THIS ????
# XXX This is not acceptable - we do not calculate things the same way in 2 different cases # XXX This is not acceptable - we do not calculate things the same way in 2 different cases
try:
attribute_value = o.getProperty(property_id)
except:
attribute_value = getattr(o,property_id, None)
REQUEST.cell = o # We need cell REQUEST.cell = o # We need cell
try: try:
value = my_field.validator.validate(my_field, key, REQUEST) # We need cell value = my_field.validator.validate(my_field, key, REQUEST) # We need cell
except ValidationError, err: # XXXX import missing error_result[error_result_key] = value
errors.append(err)
if my_field.meta_type == "MultiListField":
test_equal = 1
# Sometimes, the attribute is not a list
# so we need to force update
try: try:
for v in attribute_value: attribute_value = o.getProperty(property_id)
if v not in value:
test_equal = 0
except: except:
test_equal = 0 attribute_value = getattr(o,property_id, None)
try: if my_field.meta_type == "MultiListField":
for v in value: test_equal = 1
if v not in attribute_value: # Sometimes, the attribute is not a list
test_equal = 0 # so we need to force update
except: try:
test_equal = 0 for v in attribute_value:
else: if v not in value:
test_equal = attribute_value == value test_equal = 0
if not result.has_key(o.getUrl()): except:
result[o.getUrl()] = {} # We always provide an empty dict - this should be improved by migrating the test of equality to Bae - it is not the purpose of ListBox to do this probably. XXX test_equal = 0
if not test_equal: try:
result[o.getUrl()][sql] = value for v in value:
if v not in attribute_value:
test_equal = 0
except:
test_equal = 0
else:
test_equal = attribute_value == value
if not result.has_key(o.getUrl()):
result[o.getUrl()] = {} # We always provide an empty dict - this should be improved by migrating the test of equality to Bae - it is not the purpose of ListBox to do this probably. XXX
if not test_equal:
result[o.getUrl()][sql] = value
except ValidationError, err: # XXXX import missing
#LOG("ListBox ValidationError",0,str(err))
err.field_id = error_result_key
errors.append(err)
except: except:
LOG("ListBox WARNING",0,"Object uid %s could not be validated" % uid) LOG("ListBox WARNING",0,"Object uid %s could not be validated" % uid)
if len(errors): if len(errors) > 0:
# XXX update #LOG("ListBox FormValidationError",0,str(error_result))
pass #LOG("ListBox FormValidationError",0,str(errors))
raise FormValidationError(errors, error_result)
return result return result
ListBoxValidatorInstance = ListBoxValidator() ListBoxValidatorInstance = ListBoxValidator()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment