Commit 5362b725 authored by Jérome Perrin's avatar Jérome Perrin

Reset cell key to None in REQUEST if no cell value; Validate subfields for...

Reset cell key to None in REQUEST if no cell value; Validate subfields for editable cells. FIXME: currently only keys which contains a cell value are validated

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@6826 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 01daee33
...@@ -30,7 +30,12 @@ ...@@ -30,7 +30,12 @@
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from Products.Formulator.DummyField import fields from Products.Formulator.DummyField import fields
from Products.Formulator import Widget, Validator from Products.Formulator import Widget, Validator
from Products.Formulator.Errors import FormValidationError, ValidationError
from Products.Formulator.Field import ZMIField from Products.Formulator.Field import ZMIField
from Products.ERP5Type.Message import Message
def N_(message, **kw):
return Message('erp5_ui', message, **kw)
class MatrixBoxWidget(Widget.Widget): class MatrixBoxWidget(Widget.Widget):
""" """
...@@ -183,6 +188,7 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -183,6 +188,7 @@ class MatrixBoxWidget(Widget.Widget):
lines = field.get_value('lines') lines = field.get_value('lines')
columns = field.get_value('columns') columns = field.get_value('columns')
tabs = field.get_value('tabs') tabs = field.get_value('tabs')
field_errors = REQUEST.get('field_errors', {})
context = here context = here
getter_method_id = field.get_value('getter_method') getter_method_id = field.get_value('getter_method')
if getter_method_id not in (None,''): if getter_method_id not in (None,''):
...@@ -227,9 +233,7 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -227,9 +233,7 @@ class MatrixBoxWidget(Widget.Widget):
list_result_tab = [[tab[1]]] list_result_tab = [[tab[1]]]
# Create the header of the table - this should probably become DTML # Create the header of the table - this should probably become DTML
first_tab = tab[1] first_tab = tab[1] or ''
if first_tab is None:
first_tab = ''
header = """\ header = """\
<!-- Matrix Content --> <!-- Matrix Content -->
%s<br> %s<br>
...@@ -244,9 +248,9 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -244,9 +248,9 @@ class MatrixBoxWidget(Widget.Widget):
</td> </td>
</div> </div>
</tr> </tr>
<tr > <tr>
<td colspan="%s" width="50" align="center" valign="middle" <td colspan="%s" width="100" align="center" valign="middle"
class="DataA"> class="Data">
</td> </td>
</tr> </tr>
</table> </table>
...@@ -254,15 +258,13 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -254,15 +258,13 @@ class MatrixBoxWidget(Widget.Widget):
""" % len(columns) """ % len(columns)
list_header = """\ list_header = """\
<tr ><td class=\"Data\"></td> <tr><td class=\"Data\"></td>
""" """
for cname in columns: for cname in columns:
first_column = cname[1] first_column = cname[1] or ''
if first_column is None:
first_column = ''
list_header = list_header + ("<td class=\"Data\">%s</td>\n" % list_header = list_header + ("<td class=\"Data\">%s</td>\n" %
str(first_column)) first_column)
if render_format == 'list': if render_format == 'list':
list_result_tab[0].append(cname[1]) list_result_tab[0].append(cname[1])
...@@ -274,7 +276,6 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -274,7 +276,6 @@ class MatrixBoxWidget(Widget.Widget):
list_body = '' list_body = ''
for l in lines: for l in lines:
if not i % 2: if not i % 2:
td_css = 'DataA' td_css = 'DataA'
else: else:
...@@ -285,10 +286,8 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -285,10 +286,8 @@ class MatrixBoxWidget(Widget.Widget):
if render_format == 'list': if render_format == 'list':
list_result_lines = [ str(l[1]) ] list_result_lines = [ str(l[1]) ]
for c in columns: for c in columns:
#if column_id is None and tab_id is None: has_error = 0
# kw = []
column_id = c[0] column_id = c[0]
if (column_id is not None) and \ if (column_id is not None) and \
(not isinstance(column_id, (list, tuple))): (not isinstance(column_id, (list, tuple))):
...@@ -309,13 +308,30 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -309,13 +308,30 @@ class MatrixBoxWidget(Widget.Widget):
my_field_id = '%s_%s' % (field.id, attribute_id) my_field_id = '%s_%s' % (field.id, attribute_id)
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 = my_field.id + '_cell_%s_%s_%s' % (i,j, k) key = my_field.id + '_cell_%s_%s_%s' % (i,j,k)
if cell != None: if cell != None:
attribute_value = my_field.get_value('default', cell = cell, cell_index = kw, cell_position = (i,j, k)) attribute_value = my_field.get_value('default',
cell=cell, cell_index=kw, cell_position = (i,j,k))
if render_format=='html': if render_format=='html':
REQUEST['cell'] = cell REQUEST['cell'] = cell
cell_body += str(my_field.render(value = attribute_value, REQUEST = REQUEST, key = key)) display_value = attribute_value
if field_errors.has_key(key):
# Display previous value (in case of error)
display_value = REQUEST.get('field_%s' % key,
attribute_value)
has_error = 1
cell_body += "%s<br/>%s" % (
my_field.render(value=display_value,
REQUEST=REQUEST,
key=key),
N_(field_errors[key].error_text))
else:
cell_body += str(my_field.render(
value=attribute_value,
REQUEST=REQUEST,
key=key))
elif render_format == 'list': elif render_format == 'list':
if not my_field.get_value('hidden'): if not my_field.get_value('hidden'):
...@@ -323,23 +339,24 @@ class MatrixBoxWidget(Widget.Widget): ...@@ -323,23 +339,24 @@ class MatrixBoxWidget(Widget.Widget):
else: else:
if my_field.get_value('hidden'): if my_field.get_value('hidden'):
attribute_value = my_field.get_value('default', cell_index = kw, cell_position = (i,j, k)) attribute_value = my_field.get_value('default',
cell_index=kw, cell_position=(i,j,k))
else :
attribute_value = my_field.get_orig_value('default')
if render_format == 'html': if render_format == 'html':
cell_body += str(my_field.render(value = attribute_value, REQUEST=REQUEST, key=key)) REQUEST['cell'] = None
cell_body += str(my_field.render(value=attribute_value,
else: REQUEST=REQUEST, key=key))
if render_format == 'html':
cell_body += str(my_field.render(value = my_field.get_orig_value('default') , REQUEST=REQUEST, key=key))
elif render_format == 'list': elif render_format == 'list':
list_result_lines.append(None) list_result_lines.append(None)
css = td_css
if has_error :
css = td_css + 'Error'
list_body = list_body + \ list_body = list_body + \
('<td class=\"%s\">%s</td>' % (td_css, cell_body)) ('<td class=\"%s\">%s</td>' % (css, cell_body))
j += 1 j += 1
list_body = list_body + '</tr>' list_body = list_body + '</tr>'
i += 1 i += 1
...@@ -374,7 +391,7 @@ class MatrixBoxValidator(Validator.Validator): ...@@ -374,7 +391,7 @@ class MatrixBoxValidator(Validator.Validator):
tabs = field.get_value('tabs') tabs = field.get_value('tabs')
editable_attributes = field.get_value('editable_attributes') editable_attributes = field.get_value('editable_attributes')
getter_method_id = field.get_value('getter_method') getter_method_id = field.get_value('getter_method')
error_list = []
context = here context = here
if getter_method_id not in (None,''): if getter_method_id not in (None,''):
context = getattr(here,getter_method_id)() context = getattr(here,getter_method_id)()
...@@ -422,16 +439,21 @@ class MatrixBoxValidator(Validator.Validator): ...@@ -422,16 +439,21 @@ class MatrixBoxValidator(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)
if my_field.get_value('editable'): if my_field.get_value('editable'):
key = 'field_' + my_field.id + '_cell_%s_%s_%s' % (i,j, k) key = 'field_' + my_field.id + '_cell_%s_%s_%s' % (i,j,k)
attribute_value = my_field.get_value('default', cell = cell, cell_index = kw, attribute_value = my_field.get_value('default',
cell_position = (i,j, k)) cell=cell, cell_index=kw, cell_position = (i,j,k))
value = my_field.validator.validate(my_field, key, REQUEST) try :
value = my_field.validator.validate(
if (attribute_value != value or attribute_value not in('',None,(),[])) \ my_field, key, REQUEST)
except ValidationError, err :
err.field_id = my_field.id + '_cell_%s_%s_%s' % (i,j,k)
error_list.append(err)
if (attribute_value != value or \
attribute_value not in ('',None,(),[])) \
and not my_field.get_value('hidden'): and not my_field.get_value('hidden'):
# Only validate modified values from visible fields # Only validate modified values from visible fields
if not result.has_key(kw): result.setdefault(kw, {})
result[kw] = {}
result[kw][attribute_id] = value result[kw][attribute_id] = value
else: else:
if result.has_key(kw): if result.has_key(kw):
...@@ -439,7 +461,8 @@ class MatrixBoxValidator(Validator.Validator): ...@@ -439,7 +461,8 @@ class MatrixBoxValidator(Validator.Validator):
j += 1 j += 1
i += 1 i += 1
k += 1 k += 1
if len(error_list):
raise FormValidationError(error_list, {})
return result return result
MatrixBoxValidatorInstance = MatrixBoxValidator() MatrixBoxValidatorInstance = MatrixBoxValidator()
...@@ -462,6 +485,6 @@ class MatrixBox(ZMIField): ...@@ -462,6 +485,6 @@ class MatrixBox(ZMIField):
return ZMIField.get_value(self, id, **kw) return ZMIField.get_value(self, id, **kw)
# Psyco # Psyco
import psyco from Products.ERP5Type.PsycoWrapper import psyco
psyco.bind(MatrixBoxWidget.render) psyco.bind(MatrixBoxWidget.render)
psyco.bind(MatrixBoxValidator.validate) psyco.bind(MatrixBoxValidator.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