Hi,

I'd like to override a validator (which I know is easy) but with a twist 
- my validator needs to wrap around the automatically generated validators.

I am making a widget, GrowableRepeatingFieldSet, and I'd like the 
validator to strip out any rows that are empty. The validator I have 
seems to work fine - see below. But I am having to manually override the 
validator when creating the widget, like this:

grfs = GrowableRepeatingFieldSet(fields=ContactFields())
grfs.validator = StripBlanks(grfs.validator)

Now, I would like GrowableRepeatingFieldSet to create itself with the 
correct validator. But this is very difficult. The widget class has a 
metaclass, MetaWidget, which decorates __init__. After the custom 
__init__, for compound controls with repetitions the metaclass sets the 
validator to be ForEach.

The code I already have is below. Thanks for any help!

Paul



class GrowableRepeatingFieldSet(RepeatingFieldSet):
    javascript = [JSLink(my_static, "javascript/autogrow.js")]

    def display(self, value=None, **params):
        if value and type(value) == list:
            params['repetitions'] = len(value) + 1
        return super(AutoRepeatingFieldSet, self).display(value, **params)



class StripBlanks(validators.FancyValidator):  
    inner_validator = None   
    def __init__(self, inner_validator, **params):
        super(StripBlanks, self).__init__(**params)
        self.inner_validator = inner_validator

    def _to_python(self, value, state):
        val = self.inner_validator._to_python(value, state)
        out = []
        for i in val:
            for k,v in i.iteritems():
                if k == 'id':
                    continue
                if v:
                    out.append(i)
                    break
        #print len(out)
        return out


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to