Skip to content

Commit

Permalink
Added a real support for Archetypes required attribute
Browse files Browse the repository at this point in the history
This will probably deprecate the need of the isDataGridFilled validator
  • Loading branch information
keul committed Aug 17, 2015
1 parent d82166c commit 6ac540e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -6,6 +6,9 @@ Changelog

- Fixed demotypes registration on modern Plone (Default alias was not working)
[keul]
- Added a real support for Archetypes ``required`` attribute. This will probably deprecate
the need of the ``isDataGridFilled`` validator
[keul]

1.9.1 (2014-10-14)
------------------
Expand Down
6 changes: 6 additions & 0 deletions Products/DataGridField/DataGridField.py
Expand Up @@ -390,6 +390,12 @@ def resetFixedRows(self, instance, data):
# fixed rows behavior is disabled
return data

security.declarePrivate('validate_required')
def validate_required(self, instance, value, errors):
value = value or []
value = [d for d in value if d.get('orderindex_', '').isdigit()]
return ObjectField.validate_required(self, instance, value, errors)


class FixedRow:
""" Row which is always present at DataGridField data.
Expand Down
1 change: 1 addition & 0 deletions Products/DataGridField/examples/DataGridDemoType.py
Expand Up @@ -48,6 +48,7 @@ class DataGridDemoType(atapi.BaseContent):

DataGridField('DemoField',
searchable=True, # One unit tests checks whether text search works
required=True,
widget = DataGridWidget(),
columns=('column1','column2','The third'),
default=[
Expand Down
35 changes: 29 additions & 6 deletions Products/DataGridField/tests/test_data_manipulation.py
Expand Up @@ -63,13 +63,36 @@ def testSettingEmptyRows(self):
""" It should be possible to set empty rows """
self.folder.invokeFactory('DataGridDemoType', 'foo')

vals = ({'The third': '', 'column1': 'xxx', 'column2': '', }, )
self.folder.foo.setDemoField(vals)
self.assertEqual(self.folder.foo.getDemoField(), vals)
data = {'The third': '', 'column1': 'xxx', 'column2': ''}
raw_data = data.copy()
raw_data['orderindex_'] = '1'
self.folder.foo.setDemoField((raw_data,))
self.assertEqual(self.folder.foo.getDemoField(), (data,))

data = {'The third': '', 'column1': '', 'column2': ''}
raw_data = data.copy()
raw_data['orderindex_'] = '1'
self.folder.foo.setDemoField((raw_data,))
self.assertEqual(self.folder.foo.getDemoField(), (data,))

obj = self.folder.foo
field = obj.Schema()['DemoField']
self.assertEqual(field.validate_required(obj, (raw_data,), {}), None)

def testSettingEmptyTable(self):
""" It should not be possible to set no rows at all when field is required """
self.folder.invokeFactory('DataGridDemoType', 'foo')

vals = ({'The third': '', 'column1': '', 'column2': '', },)
self.folder.foo.setDemoField(vals)
self.assertEqual(self.folder.foo.getDemoField(), vals)
data = {'The third': '', 'column1': '', 'column2': ''}
raw_data = data.copy()
raw_data['orderindex_'] = 'template_row_marker' # whatever is not a position digit
self.folder.foo.setDemoField((raw_data,))
self.assertEqual(self.folder.foo.getDemoField(), tuple())

obj = self.folder.foo
field = obj.Schema()['DemoField']
self.assertEqual(str(field.validate_required(obj, (raw_data,), {})),
'DemoField is required, please correct.')

def testRenderingDoesNotFail(self):
"""See if a page containing DGF will output HTML without
Expand Down

0 comments on commit 6ac540e

Please sign in to comment.