Package: tgext.ajaxforms
About Ajax Formstgext.ajaxforms makes quick and easy to create ajax loaded and submitted forms in TurboGears2
applications.To make a form ajax based just apply theajaxforms.ajaxloadeddecorator to any ToscaWidgets
form and declare aajaxurlvariable to specify the method that loads the form.
The method can be automatically generated or hand-written.
By default ajax validation is automatically supported.Installingtgext.ajaxform can be installed both from pypi or from bitbucket:easy_install tgext.ajaxformsshould just work for most of the usersMaking an Ajax FormThe form itself can be any ToscaWidgets form, the only required additions
are to apply the@ajaxloadeddecorator to the form itself and
specify theajaxurlof the form that will point to the related
controller method:from tgext.ajaxforms import ajaxloaded

@ajaxloaded
class TestForm(twf.TableForm):
    class fields(WidgetsList):
        name = twf.TextField('Name', validator=validators.String(not_empty=True))
        surname = twf.TextField('Surname', validator=validators.String(not_empty=True))

    ajaxurl = '/form_show'
    action = '/form_submit'
    submit_text = "GO"
test_form = TestForm()Showing The FormTo show the form it is necessary to add a controller method bound to theajaxurlparameter of the form that will display the form itself and that will be used
by ajaxform each time that it has to display the form. For most cases this
method can be generated by using theajaxformcall.
Also you have to create a page where the form will be loaded.For example to show the form in the index page havingajaxurl = ‘/form_show’:from tgext.ajaxforms import ajaxform

class RootController(BaseController):
    form_show = ajaxform(test_form)

    @expose('myapp.templates.index')
    def index(self):
        return dict(form=test_form)Themyapp.templates.indextemplate should look the usual template
that you would use to display a ToscaWidgets based form:<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="master.html" />
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
        <title>Ajax Form Test Example</title>
    </head>
    <body>
        <div id="myform">
           ${form(name='HI')}
        </div>
    </body>
</html>Handling Submit and ValidationSubmit and validation should look the same that you would use for any ToscaWidgets
form, simply using theajaurlbound method as an error_handler:class RootController(BaseController):
    @expose()
    @validate(test_form, error_handler=form_show)
    def form_submit(self, **kw):
        return 'Thanks: {name} {surname}'.format(**kw)Complete Examplemyapp.templates.index:<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="master.html" />
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
        <title>Ajax Form Test Example</title>
    </head>
    <body>
        <div id="myform">
           ${form(name='HI')}
        </div>
    </body>
</html>myapp.controllers.root:from tgext.ajaxforms import ajaxloaded, ajaxform

@ajaxloaded
class TestForm(twf.TableForm):
    class fields(WidgetsList):
        name = twf.TextField('Name', validator=validators.String(not_empty=True))
        surname = twf.TextField('Surname', validator=validators.String(not_empty=True))

    ajaxurl = '/form_show'
    action = '/form_submit'
    submit_text = "GO"
test_form = TestForm()

class RootController(BaseController):
    form_show = ajaxform(test_form)

    @expose('myapp.templates.index')
    def index(self):
        return dict(form=test_form)

    @expose()
    @validate(test_form, error_handler=form_show)
    def form_submit(self, **kw):
        return 'Thanks: {name} {surname}'.format(**kw)Showing the Form on DemandAjaxForm since version 0.1.2 supports on demand display of forms.
This permits to show a form when a javascript event happens.To do this, you must useform.on_demand()instead ofform()to
display the form inside the template. This will create a javascript
function named:FormClassName_on_demand(where, params)thewhereargument is any valid selector where to display the form, while the
params method is a dictionary with the arguments that have to be sent
to the backend. By default any argument specified inside the params
dictionary will be used to fill the form fields.For example, to display on demand this form:@ajaxloaded
class TryAjax(TableForm):
    class fields(WidgetsList):
        field1 = TextField(validator=validators.String(not_empty=True))
        field2 = TextField(validator=validators.String(not_empty=True))
    action = '/dosomething'
    ajaxurl = '/TryAjax_show'
ajax_test = TryAjax()Your template might look like this:<div id="myform_box">
    ${form.on_demand()}
    <div onclick="TryAjax_on_demand('#myform_box', {'field2':'random text'})">Show Form</div>
</div>