Skip to content

Commit

Permalink
Enable the renaming of the submit functions, last step in the refacto…
Browse files Browse the repository at this point in the history
…r to build more than one form per page.
  • Loading branch information
ernestoalejo committed Feb 10, 2013
1 parent 55a2eca commit 0936670
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ngforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
class Form(object):
field_values = {}

def build(self, form_name='f'):
def build(self, form_name='f', submit_func='submit', try_submit_func=''):
self.form_name = form_name
self.try_submit_func = try_submit_func

fields = ''.join([f.build(self) for f in self.fields])

return """
<form class="form-horizontal" name="%s" novalidate
ng-init="%s.val = false;" ng-submit="%s.$valid && submit()">
ng-init="%s.val = false;" ng-submit="%s.$valid && %s()">
<fieldset>%s</fieldset>
</form>
""" % (form_name, form_name, form_name, fields)
""" % (form_name, form_name, form_name, submit_func, fields)

def validate(self):
request = webapp2.get_request()
Expand Down Expand Up @@ -261,10 +262,15 @@ def build(self, form):
attrs = {
"label": self.label,
}

if len(form.try_submit_func) > 0:
t = '%s(); ' % form.try_submit_func
else:
t = ''

submit = '''
<div class="form-actions">
<button ng-click="trySubmit(); %(form_name)s.val = true;"
<button ng-click="%(try)s%(form_name)s.val = true;"
class="btn btn-primary"
ng-disabled="%(form_name)s.val && !%(form_name)s.$valid">
%(label)s
Expand All @@ -273,6 +279,7 @@ def build(self, form):
''' % {
'form_name': form.form_name,
'label': self.label,
'try': t,
}

return submit

0 comments on commit 0936670

Please sign in to comment.