Skip to content

Commit

Permalink
Merge branch 'feature/buttons' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Jurman committed Mar 13, 2012
2 parents 24441df + 69a2c2b commit 94b471a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
24 changes: 24 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,30 @@ class TestLabel(WidgetTest):
attrs = {'text':'something'}
expected = """<span>something</span>"""

class TestForm(WidgetTest):
widget = Form
attrs = {'child': TableLayout(field1=TextField(id='field1')),
'buttons': [SubmitButton, ResetButton()]}
expected = """<form enctype="multipart/form-data" method="post">
<span class="error"></span>
<table >
<tr class="odd" id="field1:container">
<th>Field1</th>
<td >
<input name="field1" type="text" id="field1"/>
<span id="field1:error"></span>
</td>
</tr>
<tr class="error"><td colspan="2">
<span id=":error"></span>
</td></tr>
</table>
<input type="submit"/>
<input type="reset"/>
</form>"""
declarative = True

class TestTableForm(WidgetTest):
widget = TableForm
attrs = {'field1':TextField(id='field1'),
Expand Down
1 change: 1 addition & 0 deletions tw2/forms/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class DemoFieldSet(twf.FieldSet):

class DemoForm(twf.Form):
child = DemoTableLayout()
buttons = [twf.ResetButton()]


class DemoButton(twf.Button):
Expand Down
2 changes: 1 addition & 1 deletion tw2/forms/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
</p>
</div>
${w.child.display()}
<py:if test="w.submit">${w.submit.display()}</py:if>
<py:for each="button in w.buttons">${button.display()}</py:for>
</form>
6 changes: 3 additions & 3 deletions tw2/forms/templates/form.mak
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</div>
% endif
${w.child.display() | n}
% if w.submit:
${w.submit.display() | n}
% endif
% for button in w.buttons:
${button.display() | n}
% endfor
</form>
2 changes: 1 addition & 1 deletion tw2/forms/templates/grid_layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<tr class="error"><td colspan="${len(w.children)}" id="${w.compound_id}:error">
${w.error_msg}
</td></tr>
</table>
</table>
2 changes: 1 addition & 1 deletion tw2/forms/templates/grid_layout.mak
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
<tr class="error"><td colspan="${str(len(w.children))}" id="${w.compound_id or ''}:error">
${w.error_msg or ''}
</td></tr>
</table>
</table>
26 changes: 24 additions & 2 deletions tw2/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,23 +632,45 @@ class Form(twc.DisplayOnlyWidget):
submit = twc.Param('Submit button widget. If this is None, no submit ' +
'button is generated.',
default=SubmitButton(id='submit', value='Save'))
buttons = twc.Param('List of additional buttons to be placed at the ' +
'bottom of the form',
default=[])

attrs = {'enctype': 'multipart/form-data'}
id_suffix = 'form'

@classmethod
def post_define(cls):
if not cls.buttons:
cls.buttons = []
else:
for b in range(0, len(cls.buttons)):
if callable(cls.buttons[b]):
cls.buttons[b] = cls.buttons[b](parent=cls)

if cls.submit:
cls.submit = cls.submit(parent=cls)

def __init__(self, **kw):
super(Form, self).__init__(**kw)
if self.buttons:
for b in range(0, len(self.buttons)):
self.buttons[b] = self.buttons[b].req()

if self.submit:
self.submit = self.submit.req()

def prepare(self):
super(Form, self).prepare()
if self.submit:
self.submit.prepare()
if self.buttons and not isinstance(self.buttons, list):
raise AttributeError("buttons parameter must be a list or None")

if self.submit and not \
['SubmitButton' in repr(b) for b in self.buttons]:
self.buttons.append(self.submit)

for b in self.buttons:
b.prepare()


class FieldSet(twc.DisplayOnlyWidget):
Expand Down

1 comment on commit 94b471a

@brucecoble
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried the button changes out & they work a treat! Thanks Greg!

Please sign in to comment.