Commit 3bd55b2c authored by Sebastien Robin's avatar Sebastien Robin

patch to make Formulator returns 1 or 0 instead of True or False for

CheckBoxFields

update some patch of validation


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@172 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e2d0c78a
...@@ -63,90 +63,98 @@ Field.render = PatchedField.render ...@@ -63,90 +63,98 @@ Field.render = PatchedField.render
from Products.Formulator.Validator import SelectionValidator from Products.Formulator.Validator import SelectionValidator
class PatchedSelectionValidator(SelectionValidator): def SelectionValidator_validate(self, field, key, REQUEST):
value = StringBaseValidator.validate(self, field, key, REQUEST)
def validate(self, field, key, REQUEST):
value = StringBaseValidator.validate(self, field, key, REQUEST) if value == "" and not field.get_value('required'):
return value
if value == "" and not field.get_value('required'):
return value # get the text and the value from the list of items
# Patch by JPS for Listbox cell
# get the text and the value from the list of items for item in field.get_value('items', cell=getattr(REQUEST,'cell',None)):
# Patch by JPS for Listbox cell try:
for item in field.get_value('items', cell=getattr(REQUEST,'cell',None)): item_text, item_value = item
try: except ValueError:
item_text, item_value = item item_text = item
except ValueError: item_value = item
item_text = item
item_value = item # check if the value is equal to the string/unicode version of
# item_value; if that's the case, we can return the *original*
# check if the value is equal to the string/unicode version of # value in the list (not the submitted value). This way, integers
# item_value; if that's the case, we can return the *original* # will remain integers.
# value in the list (not the submitted value). This way, integers # XXX it is impossible with the UI currently to fill in unicode
# will remain integers. # items, but it's possible to do it with the TALES tab
# XXX it is impossible with the UI currently to fill in unicode if field.get_value('unicode') and type(item_value) == type(u''):
# items, but it's possible to do it with the TALES tab str_value = item_value.encode(field.get_form_encoding())
if field.get_value('unicode') and type(item_value) == type(u''): else:
str_value = item_value.encode(field.get_form_encoding()) str_value = str(item_value)
else:
str_value = str(item_value) if str_value == value:
return item_value
if str_value == value:
return item_value
# if we didn't find the value, return error
self.raise_error('unknown_selection', field)
SelectionValidator.validate = PatchedSelectionValidator.validate # if we didn't find the value, return error
self.raise_error('unknown_selection', field)
SelectionValidator.validate = SelectionValidator_validate
from Products.Formulator.Validator import MultiSelectionValidator from Products.Formulator.Validator import MultiSelectionValidator
class PatchedMultiSelectionValidator(MultiSelectionValidator): def MultiSelectionValidator_validate(self, field, key, REQUEST):
values = REQUEST.get(key, [])
def validate(self, field, key, REQUEST): # NOTE: a hack to deal with single item selections
values = REQUEST.get(key, []) if type(values) is not type([]):
# NOTE: a hack to deal with single item selections # put whatever we got in a list
if type(values) is not type([]): values = [values]
# put whatever we got in a list # if we selected nothing and entry is required, give error, otherwise
values = [values] # give entry list
# if we selected nothing and entry is required, give error, otherwise if len(values) == 0:
# give entry list if field.get_value('required'):
if len(values) == 0: self.raise_error('required_not_found', field)
if field.get_value('required'): else:
self.raise_error('required_not_found', field) return values
else: # convert everything to unicode if necessary
return values if field.get_value('unicode'):
# convert everything to unicode if necessary values = [unicode(value, field.get_form_encoding())
if field.get_value('unicode'): for value in values]
values = [unicode(value, field.get_form_encoding())
for value in values] # create a dictionary of possible values
value_dict = {}
# create a dictionary of possible values for item in field.get_value('items', cell=getattr(REQUEST,'cell',None)): # Patch by JPS for Listbox
value_dict = {} try:
for item in field.get_value('items', cell=getattr(REQUEST,'cell',None)): # Patch by JPS for Listbox item_text, item_value = item
try: except ValueError:
item_text, item_value = item item_text = item
except ValueError: item_value = item
item_text = item value_dict[item_value] = 0
item_value = item
value_dict[item_value] = 0 # check whether all values are in dictionary
result = []
# check whether all values are in dictionary for value in values:
result = [] # FIXME: hack to accept int values as well
for value in values: try:
# FIXME: hack to accept int values as well int_value = int(value)
try: except ValueError:
int_value = int(value) int_value = None
except ValueError: if int_value is not None and value_dict.has_key(int_value):
int_value = None result.append(int_value)
if int_value is not None and value_dict.has_key(int_value): continue
result.append(int_value) if value_dict.has_key(value):
continue result.append(value)
if value_dict.has_key(value): continue
result.append(value) self.raise_error('unknown_selection', field)
continue # everything checks out
self.raise_error('unknown_selection', field) return result
# everything checks out
return result MultiSelectionValidator.validate = MultiSelectionValidator_validate
MultiSelectionValidator.validate = PatchedMultiSelectionValidator.validate from Products.Formulator.Validator import BooleanValidator
def BooleanValidator_validate(self, field, key, REQUEST):
result = not not REQUEST.get(key, 0)
if result==True:
return 1
else:
return 0
BooleanValidator.validate = BooleanValidator_validate
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