Max Ischenko wrote:
> Hi,
>
> I have a custom Form subclass which displays different set of fields
> depending on whether user is logged in or not. It does so by displaying some
> additional fields if it determine (in update_data()) that the user is not
> logged in.
>
> The problem is that these additional fields not listed in form.fields
> attribute and therefore when the form is submitted and validated these extra
> fields are not processed. IOW, they are not validated and their content is
> not preserved when the form is redisplayed on error.
>
> One solution is to define two different forms and substituting correct form
> depending on the condition in question. It works and actually I had it that
> way. The problem with it is that this is really *one* form and I want it
> processed as such.
Hi Max,
You can use only one form and then the disabled_fields parameter, I
guess it should work but I haven't actually tested it.
Anyway:
>>> from turbogears.widgets import *
>>> class FormFields(WidgetsDeclaration):
... name = TextField()
... comment = TextField()
... password = CheckBox()
...
>>> form = ListForm(fields=FormFields())
>>> form.render()
'<FORM ACTION="" NAME="" METHOD="post">\n \n <UL>\n
<LI>\n <LABEL CLASS="fieldlabel"
FOR="name">Name</LABEL>\n <INPUT CLASS="textfield" TYPE="text"
ID="name" NAME="name">\n \n \n \n
</LI>\n \n \n <LI>\n
<LABEL CLASS="fieldlabel" FOR="comment">Comment</LABEL>\n
<INPUT CLASS="textfield" TYPE="text" ID="comment"
NAME="comment">\n \n \n \n
</LI>\n \n \n <LI>\n
<LABEL CLASS="fieldlabel" FOR="password">Password</LABEL>\n
<INPUT ID="password" TYPE="checkbox" CLASS="checkbox"
NAME="password">\n \n \n
\n </LI>\n <LI>\n <INPUT TYPE="submit"
CLASS="submitbutton">\n \n </LI>\n </UL>\n
</FORM>'
>>> form.render(disabled_fields=['name'])
'<FORM ACTION="" NAME="" METHOD="post">\n \n <UL>\n
<LI>\n <LABEL CLASS="fieldlabel"
FOR="comment">Comment</LABEL>\n <INPUT CLASS="textfield"
TYPE="text" ID="comment" NAME="comment">\n \n
\n \n </LI>\n \n
\n <LI>\n <LABEL CLASS="fieldlabel"
FOR="password">Password</LABEL>\n <INPUT ID="password"
TYPE="checkbox" CLASS="checkbox" NAME="password">\n \n
\n \n </LI>\n <LI>\n
<INPUT TYPE="submit" CLASS="submitbutton">\n \n
</LI>\n </UL>\n </FORM>'
>>>
Ciao
Michele
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---