Logo Search packages:      
Sourcecode: zope-cmfplone version File versions

def zope-cmfplone-2.0.4::FormTool::CMFForm::validate (   self,
  REQUEST,
  errors = None 
)

Executes the validator for each field in the wrapped BasicForm.add_field
Returns the results in a dictionary.

Definition at line 474 of file FormTool.py.

00474                                             :
        """
        Executes the validator for each field in the wrapped BasicForm.add_field
        Returns the results in a dictionary.
        """

        if errors is None:
            errors = REQUEST.get('errors', {})

        # This is a bit of a hack to make some of Formulator's quirks
        # transparent to developers.  Formulator expects form fields to be
        # prefixed by 'field_' in the request.  To remove this restriction,
        # we mangle the REQUEST, renaming keys from key to 'field_' + key
        # before handing off to Formulator's validators.  We will undo the
        # mangling afterwards.
        for field in self.get_fields():
            key = field.id
            value = REQUEST.get(key)
            if value:
                # get rid of the old key
                try:
                    del REQUEST[key]
                except (KeyError, AttributeError):
                    pass
                # move the old value to 'field_' + key
                # if there is already a value at 'field_' + key,
                #    move it to 'field_field_' + key, and repeat
                #    to prevent key collisions
                newKey = 'field_%s' % key
                newValue = REQUEST.get(newKey)
                while newValue:
                    REQUEST[newKey] = value
                    newKey = 'field_%s' % newKey
                    value = newValue
                    newValue = REQUEST.get(newKey)
                REQUEST[newKey] = value

        try:
            result = self.validate_all(REQUEST)
        except FormValidationError, e:
            result = {}
            for error in e.errors:
                errors[error.field.get_value('title')]=error.error_text

        # unmangle the REQUEST
        for field in self.get_fields():
            key = field.id
            value = 1
            while value:
                key = 'field_%s' % key
                value = result.get(key[6:], None) or REQUEST.get(key)
                if value:
                    REQUEST[key[6:]] = value
                    try:
                        del result[key[6:]]
                    except (KeyError, AttributeError):
                        pass
                    try:
                        del REQUEST[key]
                    except (KeyError, AttributeError):
                        pass

        return errors

InitializeClass(CMFForm)
InitializeClass(CMFForm)


Generated by  Doxygen 1.6.0   Back to index