Changeset 270

Show
Ignore:
Timestamp:
12/18/06 01:45:56 (2 years ago)
Author:
matt
Message:

Create a FormErrors? instance on the form instead of using the context to track
it. This makes the form errors more opaque, it's now just "form.errors".

Note: there is a context->IFormErrors adapter for backwards compatability for
both Formal and application code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/formal/form.py

    r269 r270  
    1818 
    1919 
     20 
     21# Backwards compatability/workaround until there's nothing left in Formal or 
     22# application code that adapts the context to IFormalErrors. 
     23def formErrorsFinder(ctx): 
     24    form = iformal.IForm(ctx) 
     25    return form.errors 
     26 
     27registerAdapter(formErrorsFinder, context.WovenContext, iformal.IFormErrors) 
     28 
     29 
     30 
    2031def renderForm(name): 
    2132 
     
    3546 
    3647            # Find errors for *this* form and remember things on the context 
    37             errors = iformal.IFormErrors(ctx, None) 
    38             if errors is not None and errors.formName == name: 
    39                 ctx.remember(errors.data, iformal.IFormData) 
     48            if form.errors: 
     49                ctx.remember(form.errors.data, iformal.IFormData) 
    4050            else: 
    41                 ctx.remember(None, iformal.IFormErrors) 
    4251                ctx.remember(form.data or {}, iformal.IFormData) 
    4352 
     
    318327        self.data = {} 
    319328        self.items = FormItems(None) 
     329        self.errors = FormErrors() 
    320330        # Forward to FormItems methods 
    321331        self.add = self.items.add 
     
    335345    def process(self, ctx): 
    336346 
    337         # Get the request args 
    338         requestArgs = inevow.IRequest(ctx).args 
    339  
    340         # Decode the request arg names 
     347        request = inevow.IRequest(ctx) 
    341348        charset = getPOSTCharset(ctx) 
    342         args = dict([(k.decode(charset),v) for k,v in requestArgs.iteritems()]) 
     349 
     350        # Get the request args and decode the arg names 
     351        args = dict([(k.decode(charset),v) for k,v in request.args.items()]) 
    343352 
    344353        # Find the callback to use, defaulting to the form default 
     
    369378                        self.actions[0].validate 
    370379 
    371         # Store an errors object in the context 
    372         errors = FormErrors(self.name) 
    373         errors.data = args 
    374         ctx.remember(errors, iformal.IFormErrors) 
     380        # Remember the args in case validation fails. 
     381        self.errors.data = args 
    375382 
    376383        # Iterate the items and collect the form data and/or errors. 
    377384        for item in self.items: 
    378             item.process(ctx, self, args, errors) 
    379  
    380         if errors and validate: 
    381             return errors 
     385            item.process(ctx, self, args, self.errors) 
     386 
     387        if self.errors and validate: 
     388            return self.errors 
    382389 
    383390        def _clearUpResources( r ): 
    384             if not errors: 
     391            if not self.errors: 
    385392                self.resourceManager.clearUpResources() 
    386393            return r 
     
    394401        e = failure.value 
    395402        failure.trap(validation.FormError, validation.FieldError) 
    396         errors = iformal.IFormErrors(ctx) 
    397         errors.add(failure.value) 
    398         return errors 
     403        self.errors.add(failure.value) 
     404        return self.errors 
    399405 
    400406 
     
    443449    implements( iformal.IFormErrors ) 
    444450 
    445     def __init__(self, formName): 
    446         self.formName = formName 
     451    def __init__(self): 
    447452        self.errors = [] 
    448453 
     
    677682 
    678683    def _renderErrors(self, ctx, data): 
    679         errors = iformal.IFormErrors(ctx, None) 
    680         if errors is not None: 
    681             errors = errors.getFormErrors() 
    682         if not errors: 
     684 
     685        if not self.original.errors: 
    683686            return '' 
     687 
     688        errors = self.original.errors.getFormErrors() 
    684689 
    685690        errorList = T.ul()