On Mon, Oct 25, 2010 at 08:45:27AM +0530, Anand Chitipothu wrote: > 2010/10/21 Daniel Garcia Moreno <[email protected]>: > > El mié, 20-10-2010 a las 13:46 -0700, Justin Davis escribió: > >> Ah, clarification, since you use "dir" you will get an alphabetical > >> list of the inputs. At least this is deterministic, though the order > >> is still not defined by the user. > >> > >> On Oct 20, 1:43 pm, Justin Davis <[email protected]> wrote: > >> > Correct me if I'm wrong, but I don't think this preserves the input > >> > order. getattr will just try to access the __dict__ of the object (and > >> > then the parent, etc), but it won't preserve order of the output. This > >> > means that calling render on that object will produce weird layouts. > >> > > > > > I fix that, now fields are ordered. I also make the sintax more clear, > > andrei commented that it would be much better if you didn't have to name > > your inputs twice, and I fixed it too. But for that I need to create > > other classes inside form because Input requires name param in __init__ > > method. > > > > The new syntax is like that: > > > > from web import form > > > > > > class MyForm(form.Form): > > name = form.TextboxField() > > password = form.PasswordField() > > password2 = form.PasswordField() > > > > validator1 = form.Validator("Passwords didn't match.", lambda i: > > i.password == i.password2) > > > > And you can define validators as a list using __validators__ attr. > > > > As you can see, I don't use normal form.Input, I use inputname plus > > Field that is a class that inherit from the Input but has an __init__ > > method that don't need name as first argument. > > Looks good. Couple of comments: > > * Can you try implementing this without using metaclass magic? > > * I think it is better to specify validators using attribute > "validators" instead of adding each validator as an attribute or using > __validators__. Like this: > > class MyForm(form.Form): > name = form.TextboxField() > password = form.PasswordField() > password2 = form.PasswordField() > > validators = [ > form.Validator("Passwords didn't match.", lambda i: i.password > == i.password2) > ]
But without metaclass magic we need to write one "Field" class for each Input, rewriting only the __init__ method to pass a blank name... Maybe there's another method to make all inputs instantiables without name. I thought make name an optional attribute in __init__ but there's *validators, and we can't put named attributes before ordered attributes...
signature.asc
Description: Digital signature
