Alberto Valverde wrote:
>
> *Very bad idea*. This will lead to unexpected behavior as you're
> changing the validator of a widget that could be reused in other
> forms. Example:
>
> from turbogears.widgets import *
> from turbogears.validators import *
>
> class PasswordFields(WidgetsList):
> password = PasswordField()
> password_confirm = PasswordField()
>
> class Passwords(FieldSet):
> name = "passwords"
> fields = PasswordFields()
>
> class Schema1(Schema):
> chained_validators = [FieldsMatch("password", "password_confirm")]
> password = String(min=6)
> password_confirm = String(min=6)
>
> class Schema2(Schema):
> chained_validators = [FieldsMatch("password", "password_confirm")]
> password = String(min=8)
> password_confirm = String(min=8)
>
> form1 = ListForm(fields=[Passwords(validator=Schema1())])
> form2 = ListForm(fields=[Passwords(validator=Schema2())])
> form3 = ListForm(fields=[Passwords()])
>
> form1.validate(dict(passwords=dict(password="aaaaaa",
> password_confirm="aaaaaa")))
> form2.validate(dict(passwords=dict(password="aaaaaaaa",
> password_confirm="aaaaaaaa")))
> form3.validate(dict(passwords=dict(password="aaaaaa",
> password_confirm="aaaaaa")))
>
> form3 will have validation and I don't want it. It will make sure
> passwords match and are at least 8 chars long, just because form1 is
> defined before form2, else they will be 6 chars. If this is expected
> behavior you might as well remove copy_schema and give us a big speed
> boost as it's not needed anymore then.
>
*Houston we have a problem* :D
Hi Alberto,
thanks again for the feedback.
Yes, you're right this could lead to unexpected behaviors, OTOH we have
an unexpected behavior in both case since the validator of our widget
doesn't respect the one that's being used for validating it.
What should we do here? is the use case you're describing so common?
should we let people reuse the same widgets over and over for different
forms in this way?
We need to solve this, having self.validator != real validator is
something we can't accept IMHO, the only solution I see that can give
us a coherent and predicable behavior without searching for very
difficult solutions is using the metaclass to walk every widget used by
a compound widget and setting a flag on it (for example _in_use), when
a new compound widget is declared we check this flag and it is True we
issue an error.
Other ideas? using a property for self.validator is not a viable
solution since we end up with an infinite recursion and we can't use it
for normal CompoundWidget(s).
Thanks again.
Ciao
Michele
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" 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-trunk
-~----------~----~----~----~------~----~------~--~---