def validate(self, data=None, from_flat=True): """Validate the form against the data passed. If no data is provided the form data of the current request is taken. By default a flat representation of the data is assumed. If you already have a non-flat representation of the data (JSON for example) you can disable that with ``from_flat=False``. """ if data is None: data = self._autodiscover_data() if from_flat: data = decode_form_data(data) self.raw_data = data # for each field in the root that requires validation on value # omission we add `None` into the raw data dict. Because the # implicit switch between initial data and user submitted data # only happens on the "root level" for obvious reasons we only # have to hook the data in here. for name, field in self._root_field.fields.iteritems(): if field.validate_on_omission and name not in self.raw_data: self.raw_data.setdefault(name) d = self.data.copy() d.update(self.raw_data) errors = {} try: data = self._root_field(d) except ValidationError, e: errors = e.unpack(self)
def test_decode_list_in_dict(self): d = utils.decode_form_data({ 'a_list': ['foo', 'bar'], 'a_list.42': 'baz', 'a_list.23': 'meh' }) self.assertEqual(d['a_list'], ['foo', 'bar', 'meh', 'baz'])
def test_decode_form_data_multidicts(self): for dcls in WebObLikeDict, WerkzeugLikeDict: d = utils.decode_form_data(dcls()) self.assertEqual(d['key1'], ['value1', 'value2', 'value3']) self.assertEqual(d['key2'], 'awesome')