コード例 #1
0
ファイル: testValidators.py プロジェクト: a25kk/stv2
    def test_messageMassage(self):
    
        s = "Validation failed(isEmail): something is not a valid email address."
        self.failUnlessEqual(validationMessages.cleanupMessage(s, self), 'This is not a valid email address.')

        s = "Something is required, please correct."
        self.failUnlessEqual(validationMessages.cleanupMessage(s, self), 'This field is required.')
コード例 #2
0
    def test_messageMassage(self):

        s = "Validation failed(isUnixLikeName): something is not a valid identifier."
        self.failUnlessEqual(validationMessages.cleanupMessage(s, self, self), u"pfg_isUnixLikeName")

        s = "Something is required, please correct."
        self.failUnlessEqual(validationMessages.cleanupMessage(s, self, self), u"pfg_isRequired")

        s = "Validation failed(isNotTooLong): 'something' is too long. Must be no longer than some characters."
        response = validationMessages.cleanupMessage(s, self, self)
        self.failUnlessEqual(response, u"pfg_too_long")
コード例 #3
0
    def test_messageMassage(self):

        # s = "Validation failed(isUnixLikeName): something is not a valid identifier."
        # self.failUnlessEqual(validationMessages.cleanupMessage(s, self, self), u'pfg_isUnixLikeName')

        s = "Something is required, please correct."
        self.failUnlessEqual(validationMessages.cleanupMessage(s, self, self),
                             u'pfg_isRequired')

        s = "Validation failed(isNotTooLong): 'something' is too long. Must be no longer than some characters."
        response = validationMessages.cleanupMessage(s, self, self)
        self.failUnlessEqual(response, u'pfg_too_long')
コード例 #4
0
    def fgvalidate(self, REQUEST=None, errors=None, data=None, metadata=None):
        # Validates the field data from the request.

        _marker = []
        if errors is None:
            errors = {}
        if errors:
            return errors

        field = self.fgField

        result = field.widget.process_form(self, field, REQUEST.form, empty_marker=_marker)

        if result is None or result is _marker:
            #XXX Make this smarter
            value = ''
        else:
            value = result[0]

        # workaround what I consider a Zope marshalling error: the production
        # of lists like ['one', ''] and [''].
        # no need to worry about polymorphism here, as this is a very particular
        # case.
        if isinstance(value, type([])) and len(value) and \
            (type(value[-1]) in StringTypes) and (len(value[-1]) == 0):
            value.pop()

        # eliminate trailing white space in string types.
        if safe_hasattr(value, 'rstrip'):
            newvalue = value.rstrip()
            if newvalue != value:
                value = newvalue
                # since strings are immutable, we have to manually store it back to the request
                if safe_hasattr(REQUEST, 'form'):
                    REQUEST.form[self.getFieldFormName()] = value

        # Archetypes field validation
        res = field.validate(instance=self,
                            value=value,
                            errors=errors,
                            REQUEST=REQUEST)

        if not res:
            # give the field itself an opportunity to validate.
            res = self.specialValidator(value, field, REQUEST, errors)

        if res:
            errors[field.getName()] = validationMessages.cleanupMessage(res, self.REQUEST, self)
        elif safe_hasattr(self, 'getFgTValidator') and self.getRawFgTValidator():
            # process the override validator TALES expression

            # create a context for expression evaluation.
            # Note that we're explicitly passing the form object to getExprContext;
            # this makes sure we don't cache the wrong context.
            context = getExprContext(self, self.aq_parent)

            # put this field's input (from request) into the context globals
            # as 'value'
            context.setGlobal('value', REQUEST.form.get(self.getFieldFormName(), None))

            # call the tales expression, passing our custom context
            cerr = self.getFgTValidator(expression_context=context)
            if cerr:
                errors[field.getName()] = cerr

        return errors
コード例 #5
0
ファイル: form.py プロジェクト: arielvb/Products.PloneFormGen
    def fgvalidate(self,
                   REQUEST=None,
                   errors=None,
                   data=None,
                   metadata=None,
                   skip_action_adapters=False):
        """Validates the field data from the request.
        """

        if getattr(self, 'checkAuthenticator', True):
            # CSRF check.
            plone.protect.CheckAuthenticator(REQUEST)
            plone.protect.PostOnly(REQUEST)

        _marker = []
        if errors is None:
            errors = {}
        if errors:
            return errors

        # Get all the form fields. Exclude actual IField fields.
        fields = [fo for fo in self._getFieldObjects()
                  if not implementedOrProvidedBy(IField, fo)]
        for obj in fields:
            field = obj.fgField

            if obj.isLabel() and obj.meta_type != 'FormRichLabelField':
                REQUEST.form[obj.__name__] = '1'

            if obj.getServerSide():
                # for server-side only fields, use the default value
                # even if something was in the request
                if obj.__name__ in REQUEST.form:
                    del REQUEST.form[obj.__name__]
                obj.fgPrimeDefaults(REQUEST)

            result = field.widget.process_form(self, field, REQUEST.form,
                                               empty_marker=_marker)

            if result is None or result is _marker:
                #XXX Make this smarter
                value = ''
            else:
                value = result[0]

            # workaround what I consider a Zope marshalling error:
            # the production of lists like ['one', ''] and ['']
            # for list fields. No need to worry about polymorphism here,
            # as this is a very particular case.
            if isinstance(value, type([])) and len(value) and \
              (type(value[-1]) in StringTypes) and (len(value[-1]) == 0):
                value.pop()

            # eliminate trailing white space in string types.
            if safe_hasattr(value, 'rstrip'):
                newvalue = value.rstrip()
                if newvalue != value:
                    value = newvalue
                    # since strings are immutable,
                    # we have to manually store it back to the request
                    if safe_hasattr(REQUEST, 'form'):
                        REQUEST.form[obj.getFieldFormName()] = value

            # Archetypes field validation
            res = field.validate(instance=self, value=value, errors=errors,
                                 REQUEST=REQUEST)

            if not res:
                # give the field itself an opportunity to validate.
                res = obj.specialValidator(value, field, REQUEST, errors)

            if res:
                errors[field.getName()] = \
                  validationMessages.cleanupMessage(res, self.REQUEST, self)
            elif shasattr(obj, 'getFgTValidator') and obj.getRawFgTValidator():
                # process the override validator TALES expression

                # create a context for expression evaluation
                context = getExprContext(self, obj)

                # put this field's input (from request)
                # into the context globals as 'value'
                context.setGlobal('value',
                                  REQUEST.form.get(obj.getFieldFormName(),
                                  None))

                # call the tales expression, passing our custom context
                cerr = obj.getFgTValidator(expression_context=context)
                if cerr:
                    errors[field.getName()] = cerr

        if not skip_action_adapters:
            return self.fgProcessActionAdapters(errors, fields, REQUEST)

        return errors
コード例 #6
0
    def fgvalidate(self, REQUEST=None, errors=None, data=None, metadata=None):
        """Validates the field data from the request.
        """

        _marker = []
        if errors is None:
            errors = {}
        if errors:
            return errors

        field = self.fgField

        result = field.widget.process_form(self,
                                           field,
                                           REQUEST.form,
                                           empty_marker=_marker)

        if result is None or result is _marker:
            #XXX Make this smarter
            value = ''
        else:
            value = result[0]

        # workaround what I consider a Zope marshalling error: the production
        # of lists like ['one', ''] and [''].
        # no need to worry about polymorphism here, as this is a very particular
        # case.
        if isinstance(value, type([])) and len(value) and \
            (type(value[-1]) in StringTypes) and (len(value[-1]) == 0):
            value.pop()

        # eliminate trailing white space in string types.
        if safe_hasattr(value, 'rstrip'):
            newvalue = value.rstrip()
            if newvalue != value:
                value = newvalue
                # since strings are immutable, we have to manually store it back to the request
                if safe_hasattr(REQUEST, 'form'):
                    REQUEST.form[self.getFieldFormName()] = value

        # Archetypes field validation
        res = field.validate(instance=self,
                             value=value,
                             errors=errors,
                             REQUEST=REQUEST)

        if not res:
            # give the field itself an opportunity to validate.
            res = self.specialValidator(value, field, REQUEST, errors)

        if res:
            errors[field.getName()] = validationMessages.cleanupMessage(
                res, self.REQUEST, self)
        elif safe_hasattr(self,
                          'getFgTValidator') and self.getRawFgTValidator():
            # process the override validator TALES expression

            # create a context for expression evaluation.
            # Note that we're explicitly passing the form object to getExprContext;
            # this makes sure we don't cache the wrong context.
            context = getExprContext(self, self.aq_parent)

            # put this field's input (from request) into the context globals
            # as 'value'
            context.setGlobal('value',
                              REQUEST.form.get(self.getFieldFormName(), None))

            # call the tales expression, passing our custom context
            cerr = self.getFgTValidator(expression_context=context)
            if cerr:
                errors[field.getName()] = cerr

        return errors
コード例 #7
0
    def fgvalidate(self,
                   REQUEST=None,
                   errors=None,
                   data=None,
                   metadata=None,
                   skip_action_adapters=False):
        """Validates the field data from the request.
        """

        if getattr(self, 'checkAuthenticator', True):
            # CSRF check.
            plone.protect.CheckAuthenticator(REQUEST)
            plone.protect.PostOnly(REQUEST)

        _marker = []
        if errors is None:
            errors = {}
        if errors:
            return errors

        # Get all the form fields. Exclude actual IField fields.
        fields = [
            fo for fo in self._getFieldObjects()
            if not implementedOrProvidedBy(IField, fo)
        ]
        for obj in fields:
            field = obj.fgField

            if obj.isLabel() and obj.meta_type != 'FormRichLabelField':
                REQUEST.form[obj.__name__] = '1'

            if obj.getServerSide():
                # for server-side only fields, use the default value
                # even if something was in the request
                if obj.__name__ in REQUEST.form:
                    del REQUEST.form[obj.__name__]
                obj.fgPrimeDefaults(REQUEST)

            result = field.widget.process_form(self,
                                               field,
                                               REQUEST.form,
                                               empty_marker=_marker)

            if result is None or result is _marker:
                #XXX Make this smarter
                value = ''
            else:
                value = result[0]

            # workaround what I consider a Zope marshalling error:
            # the production of lists like ['one', ''] and ['']
            # for list fields. No need to worry about polymorphism here,
            # as this is a very particular case.
            if isinstance(value, type([])) and len(value) and \
              (type(value[-1]) in StringTypes) and (len(value[-1]) == 0):
                value.pop()

            # eliminate trailing white space in string types.
            if safe_hasattr(value, 'rstrip'):
                newvalue = value.rstrip()
                if newvalue != value:
                    value = newvalue
                    # since strings are immutable,
                    # we have to manually store it back to the request
                    if safe_hasattr(REQUEST, 'form'):
                        REQUEST.form[obj.getFieldFormName()] = value

            # Archetypes field validation
            res = field.validate(instance=self,
                                 value=value,
                                 errors=errors,
                                 REQUEST=REQUEST)

            if not res:
                # give the field itself an opportunity to validate.
                res = obj.specialValidator(value, field, REQUEST, errors)

            if res:
                errors[field.getName()] = \
                  validationMessages.cleanupMessage(res, self.REQUEST, self)
            elif shasattr(obj, 'getFgTValidator') and obj.getRawFgTValidator():
                # process the override validator TALES expression

                # create a context for expression evaluation
                context = getExprContext(self, obj)

                # put this field's input (from request)
                # into the context globals as 'value'
                context.setGlobal(
                    'value', REQUEST.form.get(obj.getFieldFormName(), None))

                # call the tales expression, passing our custom context
                cerr = obj.getFgTValidator(expression_context=context)
                if cerr:
                    errors[field.getName()] = cerr

        if not skip_action_adapters:
            return self.fgProcessActionAdapters(errors, fields, REQUEST)

        return errors
コード例 #8
0
ファイル: form.py プロジェクト: dtgit/dtedu
    def fgvalidate(self, REQUEST=None, errors=None, data=None, metadata=None):
        """Validates the field data from the request.
        """

        _marker = []
        if errors is None:
            errors = {}
        if errors:
            return errors

        # Get all the form fields. Exclude actual IField fields.
        fields = [fo for fo in self._getFieldObjects() if not IField.isImplementedBy(fo)]

        for obj in fields:
            field = obj.fgField

            result = field.widget.process_form(self, field, REQUEST.form, empty_marker=_marker)

            if result is None or result is _marker:
                #XXX Make this smarter
                value = ''
            else:
                value = result[0]

            # workaround what I consider a Zope marshalling error: the production
            # of lists like ['one', ''] and [''] for list fields.
            # no need to worry about polymorphism here, as this is a very particular
            # case.        
            if isinstance(value, type([])) and len(value) and \
              (type(value[-1]) in StringTypes) and (len(value[-1]) == 0):
                value.pop()
                
            # eliminate trailing white space in string types.
            if safe_hasattr(value, 'rstrip'):
                newvalue = value.rstrip()
                if newvalue != value:
                    value = newvalue
                    # since strings are immutable, we have to manually store it back to the request
                    if safe_hasattr(REQUEST, 'form'):
                        REQUEST.form[obj.getFieldFormName()] = value                        

            # Archetypes field validation
            res = field.validate(instance=self, value=value, errors=errors, REQUEST=REQUEST)

            if not res:
                # give the field itself an opportunity to validate.
                res = obj.specialValidator(value, field, REQUEST, errors)

            if res:
                errors[field.getName()] = validationMessages.cleanupMessage(res, self.REQUEST, self)
            elif shasattr(obj, 'getFgTValidator') and obj.getRawFgTValidator():
                # process the override validator TALES expression

                # create a context for expression evaluation
                context = getExprContext(self, obj)
                
                # put this field's input (from request) into the context globals
                # as 'value'
                context.setGlobal('value', REQUEST.form.get(obj.getFieldFormName(), None))
                
                # call the tales expression, passing our custom context
                cerr = obj.getFgTValidator(expression_context=context)
                if cerr:
                    errors[field.getName()] = cerr

        if not errors:
            if self.getRawAfterValidationOverride():
                # evaluate the override.
                # In case we end up traversing to a template,
                # we need to make sure we don't clobber
                # the expression context.
                self.getAfterValidationOverride()
                self.cleanExpressionContext(request=self.REQUEST)

            adapters = self.getRawActionAdapter()
            for adapter in adapters:
                actionAdapter = getattr(self.aq_explicit, adapter, None)
                if actionAdapter is None:
                    logger.warn("Designated action adapter '%s' is missing; ignored." % adapter)
                else:
                    # Now, see if we should execute it.
                    # Check to see if execCondition exists and has contents
                    if safe_hasattr(actionAdapter, 'execCondition') and \
                      len(actionAdapter.getRawExecCondition()):
                        # evaluate the execCondition.
                        # create a context for expression evaluation
                        context = getExprContext(self, actionAdapter)
                        doit = actionAdapter.getExecCondition(expression_context=context)
                    else:
                        # no reason not to go ahead
                        doit = True

                    if doit:
                        result = actionAdapter.onSuccess(fields, REQUEST=REQUEST)
                        if type(result) is type({}) and len(result):
                            # return the dict, which hopefully uses
                            # field ids or FORM_ERROR_MARKER for keys
                            return result

        return errors