On Nov 17, 2006, at 3:36 PM, miya wrote:
>
> Hi, I'm trying to create a interface for the User model, with the
> aggregate of the user_surname field.
> The problem I'm having is with the update interface. I already did the
> create one, and I wish to re-use it for the Update interface.
>
> The thing is that I cant populate the TextFields of the Form. I know
> that I have to use the default field of the TextWidget, but I just
> can't make it work.
>
> (May be this is a more 'python list' question, if this is the case,
> please let me know)
>
> The widgetList i'm subclassing is this one:
>
> // ----------------------------------------------------
>
> class UserFormList(widgets.WidgetsList):
> default_user_name = None
> def __init__(self, arg_user_name = 'hola'):
> widgets.WidgetsList.__init__(self)
> default_user_name = 'asdfasdf'
> return
>
> user_name = widgets.TextField(
> name = 'user_name',
> label = 'Nombre',
> default = default_user_name,
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, v.UnicodeString))
> user_surname = widgets.TextField(
> name = 'user_surname',
> label = 'Apellido',
> attrs={'size': 32, 'maxlength': 255 },
> validator=v.All(v.NotEmpty, v.UnicodeString ))
> email_address = widgets.TextField(
> name='email_address', label='Correo
> electronico',
> attrs={'size': 32, 'maxlength': 64},
> validator=v.All(v.NotEmpty, v.Email,
> userKeyExists(field =
> '_email_address')))
> display_name = widgets.TextField(
> name = 'display_name',
> label = 'Nick',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty,
> userKeyExists(field=
> '_display_name')))
> password = widgets.PasswordField(
> name = 'password',
> label = 'Contrasena',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, v.UnicodeString ))
> passwordDuplicate = widgets.PasswordField(
> name = 'passwordDuplicate',
> label = 'Verificacion contrasena',
> attrs={'size': 32, 'maxlength': 255},
> validator=v.All(v.NotEmpty, v.UnicodeString))
> submit_text = "Ingresar usuario"
>
> insert_user_form= widgets.TableForm(fields=UserFormList(),
> validator =
> v.Schema(chained_validators=[v.FieldsMatch("password",
> "passwordDuplicate")]))
>
> // -----------------------------
>
> I just want to make the default text of user_name to be 'asdfasdf'.
> First I declare/define the 'default_user_name' to None, and then, in
> the constructor I assign 'asdfasdf' to it. But, when I do a "default =
> default_user_name", default_user_name looses the 'asdfasdf' value.
>
> I really don't know whats going on here...please help!
>
> thanks for your time.
No no no no no no! :)
WidgetsLists are *only* a nicer syntax for declaring a list of
widgets. The following snippets are equivalent:
>>> widgets = [
... TextField("name"),
... TextField("age"),
... ]
>>> class Widgets(WidgetsList):
... name = TextField()
... age = TextField()
>>> widgets1 = Widgets()
>>> widgets == widgets1
True
WidgetsLists are meant to be just that: "syntactic sugar" for
declaring widgets to be passed to a form or to a fieldset. You should
not try to override any methods in a WidgetsLists because there's
some "magic" going on behind scenes (by virtue of a metaclass) which
was only designed to pop those attributes and place them in the same
order as declared in a list.
What I think you're trying to accomplish is to facade "user_name"s
initialization through the form constructor, right?
Assuming that's the case, one way to do that would be to subclass a
Form itself and initialize it's fields in the constructor:
class UserForm(TableForm):
def __init__(self, *args, **kw):
default_user_name = kw.pop('arg_user_name', 'hola')
kw['fields'] = [
TextField('user_name', default=default_user_name, ...),
....
]
super(UserForm, self).__init__(*args, **kw)
Now you can create a form with 'pepe' as a default user name by
initializing UserForm like:
pepe_form = UserForm(default_user_name='pepe')
However, I wouldn't really do that because you would need to
instantiate a form for every user... to set a default value for a
field when a form is rendered it's better to just declare and
initialize the form once and send a value to it through your
controller method:
class UserController(...):
@expose(...)
def edit_user(self, ...):
....
return dict(form=user_form, value={'user_name':'pepe'})
and in your template:
${form.display(value)}
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?hl=en
-~----------~----~----~----~------~----~------~--~---