On 7/17/06, Alberto Valverde <[EMAIL PROTECTED]> wrote:
I hope the reason is the latter. I tried creating a validator (subclassed from Schema) and passing it to the compound widget, but nothing,.. well, here is my code:
first, the widgets:
%%%%%%%%%%%% %< %%%%%%%%%%%%%
class SinglePrettySelectField(wdg.SingleSelectField):
template = """
<select xmlns:py="http://purl.org/kid/ns#"
size="${size}"
name="${name}"
class="${field_class}"
id="${field_id}"
py:attrs="attrs"
>
<optgroup py:for="" options in grouped_options"
label="${group}"
py:strip="not group"
>
<option py:for="" desc, attrs in options"
value="${value}"
py:attrs="attrs"
py:content="desc"
/>
</optgroup>
</select>
"""
params=["size"]
class SingleSelectWithControls(wdg.CompoundFormField):
template="""
<table xmlns:py="http://purl.org/kid/ns#" border="0" cellspacing="0"><tr><td>
${display_field_for(select_widget)}</td>
<td>
<table border="0" cellspacing="0">
<tr><td>${display_field_for(button_add)}</td></tr>
<tr><td>${display_field_for(button_delete)}</td></tr>
</table>
</td></tr>
<tr>
</tr>
</table>
"""
member_widgets=['select_widget','button_add','button_delete']
%%%%%%%%%%%% %< %%%%%%%%%%%%%
and now the controller:
%%%%%%%%%%%% %< %%%%%%%%%%%%%
f=w.TableForm(fields=[
lw.SingleSelectWithControls(label="Algo",
select_widget=lw.SinglePrettySelectField(name="opt",label="Options",size=5,options=[(i,str(i)) for i in range(5) ],validator=v.Int(not_empty=True)),
button_add=w.Button(default="Add...",name="add"),
button_delete=w.Button(default="Delete...",name="delete")
),
lw.SinglePrettySelectField(name="opt2",label="Options",size=5,options=[(i,str(i)) for i in range(5) ],validator=v.Int(not_empty=True))
],
submit_text=u"Push Me!!")
class Root(tg.controllers.RootController):
@tg.expose(template="forms.templates.forms")
def index(self):
global f
return dict(form=f,title=u"Hello!!!",action="">
@tg.expose(format="json")
@tg.validate(form=f)
@tg.error_handler(index)
def put(self,**kwargs):
return kwargs
%%%%%%%%%%%% %< %%%%%%%%%%%%%
> other question:
> how can I use validators with compound widgets? I've defined
> validators for
> all the elements within the compound widget, but they don't seem to
> work.
Validators for compound widgets should subclass
turbogears.validators.Schema. If you don't define one for the
compound widget itself then a schema will be automatically generated
including every validator present in the child widgets. If this is
not working for you, can you please submit some code that presents
this behavior? Maybe you've found a bug or maybe you're just doing
something wrong... ;)
I hope the reason is the latter. I tried creating a validator (subclassed from Schema) and passing it to the compound widget, but nothing,.. well, here is my code:
first, the widgets:
%%%%%%%%%%%% %< %%%%%%%%%%%%%
class SinglePrettySelectField(wdg.SingleSelectField):
template = """
<select xmlns:py="http://purl.org/kid/ns#"
size="${size}"
name="${name}"
class="${field_class}"
id="${field_id}"
py:attrs="attrs"
>
<optgroup py:for="" options in grouped_options"
label="${group}"
py:strip="not group"
>
<option py:for="" desc, attrs in options"
value="${value}"
py:attrs="attrs"
py:content="desc"
/>
</optgroup>
</select>
"""
params=["size"]
class SingleSelectWithControls(wdg.CompoundFormField):
template="""
<table xmlns:py="http://purl.org/kid/ns#" border="0" cellspacing="0"><tr><td>
${display_field_for(select_widget)}</td>
<td>
<table border="0" cellspacing="0">
<tr><td>${display_field_for(button_add)}</td></tr>
<tr><td>${display_field_for(button_delete)}</td></tr>
</table>
</td></tr>
<tr>
</tr>
</table>
"""
member_widgets=['select_widget','button_add','button_delete']
%%%%%%%%%%%% %< %%%%%%%%%%%%%
and now the controller:
%%%%%%%%%%%% %< %%%%%%%%%%%%%
f=w.TableForm(fields=[
lw.SingleSelectWithControls(label="Algo",
select_widget=lw.SinglePrettySelectField(name="opt",label="Options",size=5,options=[(i,str(i)) for i in range(5) ],validator=v.Int(not_empty=True)),
button_add=w.Button(default="Add...",name="add"),
button_delete=w.Button(default="Delete...",name="delete")
),
lw.SinglePrettySelectField(name="opt2",label="Options",size=5,options=[(i,str(i)) for i in range(5) ],validator=v.Int(not_empty=True))
],
submit_text=u"Push Me!!")
class Root(tg.controllers.RootController):
@tg.expose(template="forms.templates.forms")
def index(self):
global f
return dict(form=f,title=u"Hello!!!",action="">
@tg.expose(format="json")
@tg.validate(form=f)
@tg.error_handler(index)
def put(self,**kwargs):
return kwargs
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

