On 03/06/2006, at 23:34, Alberto Valverde wrote:

>
> On 03/06/2006, at 19:45, Joseph Wenninger wrote:
>> Hi
>>
>> I've a form with nested compund widgets and I'm a littlebit lost
>> about how to
>> write a "complex" validation schema.
>>
>> Simplyfied the form result looks like the following dictionary.
>>
>> { 'basics': {'defaultgateway': {'ip_part_4': 111, 'ip_part_1': 111,
>> 'ip_part_3': 111, 'ip_part_2': 111}, 'enabledisable': True}}
>>
>>
>> I'd like to write a validation schema, which returns valid if:
>> *) enabledisable in 'basics' is True and all the ip_part fields in  
>> the
>> 'defaultgateway' are not empty
>> *) enabledisable is False the ip_part fields should not be
>> validated (they
>> should be ignored) and the result should be true in any case
>>
>> My problem is I couldn't find a documentation how to write a nested
>> validation
>> schema. I can write a schema for single fields, or for one compound
>> widget,
>> but I can't figure out how to validate the widgets of two levels at
>> the same
>> time.
>>
>> Thanks in advance for any help
>
> You could implement this with help of the 'state' parameter FE
> validator methods accept. Basically it's an instance that will get
> passed through all child validators where you can store... state :)
>
> Unfortunately the 'validate' decorator where the form's validator
> gets called doesn't pass any 'state' parameter when validating ATM as
> it is very application specific.
>
> Since you've got a use case here I think I might be able to commit a
> patch for this on Monday (no promises though). Im thinking of
> something like a config option (tg.validation_state_factory?) which
> points to a callable (a là identity.user_class) which returns an app-
> specific object where state can be passed.


FYI, I've committed at [1526] an implementation for this. You should  
be able to accomplish what you're trying with something *like* this  
(untested):

class BasicsValidator(Schema):
        def _to_python(self, value, state=None):
                state.enabled = value['enabledisable']
                ....
class DefaultGWValidator(Schema):
        def to_python(self, value, state=None):
                if state.enabled:
                        return super(....).to_python(....)
                else:
                        return value

class MyState(object):
        enabled = False

@validate(validators=XXXX, state_factory=MyState)
# XXXX is a schema that contains both validators mentioned above
@expose()
def controllermethod(self, **kw):
        ....

This should work as long as BasicsValidator is called *before*  
DefaultGWValidator (to set state.enabled properly).

For more info feel free to ask, not before taking a peek at the  
unittests and the docstrings ;)

HTH,
Alberto
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to