Working with forms
Basic usage
Usually almost each site require to interact with a user, so you need different types of forms for working with data. Tabbli supports a system for automatic form rendering, validation and submission. In most cases you can use method get_record_form(...)
from DB
interface. Please see Working with the database section for understand principles for working with data. The basic scenario for forms is like this:
Create a form object for the collection
Check the request method and try to validate the form if it is POST
If validation is passed then save a data. Do not forget to execute scenarios for a proper trigger with
execute_scenario(...)
method.Render the form if you need it (for GET request or if validation has not been passed)
Make a redirect to another page after successful form submission
Look at this example:
If you need to edit existing record you need to specify record
attribute for get_record_form
:
For cases when you use File properties do not forge to use enctype="multipart/form-data" attribute for the form tag:
Modify submitted data before saving to the database
If a form returns only a part of required property values you need for successful saving a record to the database, you can use form.save(commit=False) for creating a record object but without saving to the database. Modify the record and then use its save() method for update the database.
As you see you should use update(...)
method for change the properties, because {% set ... %}
tag allows to modify variables only inside the template context.
If you need to change some system properties, like name
for example, you should use setattr
function:
Hiding "Name" field
If you want to generate a value for Name field automatically or using computation options for it, you can use attribute ignore_name:
Show only specified fields
If you want to create a form with only specified fields (for example, if you want to fill the rest of properties automatically) you should specify property_keys
attribute:
Widgets overriding
Some types of properties (like relations) support alternate widgets. You can such code for change it:
The next alternate widgets are available:
For single item selection:
default - autocomplete widget;
select - just a regular select box;
radio_select - radio buttons.
For multiple items selection:
default - autocomplete widget;
select_multiple - regular selectbox with multiple selection support;
checkbox_select - a list of checkboxes.
Submitting forms via AJAX
In some cases it might sense to work with forms via AJAX, without reloading a whole page in the browser. Tabbli provides a simple JS framework for work with such kind of behavior which is called TabbliJS which is included to a standard site template. First of all, you need to add a container to any page where you need to show the form:
Here the class __actiom--ajax-load
is using to tell the Tabbli to load the page with URL specified in the attribute data-src
. After that you should create additional controller and a template which renders the form itself.
In that code, class __action--ajax-submit
means that after pressing Submit button the form data will be submitted via AJAX. When server will generate a result it will loaded into the container with id form-container
(it is specified in attribute data-target
). You can see that it is a container in our main page where we are loading the form into. If form validation is passed the result will contain an alert message about successful submission.
Changing standard forms look and feel
Sometimes you could be interested in overriding basic Tabbli templates for form rendering. If you use Bootstrap 4 as your CSS framework you rarely will want to do it. But, if you really need it you can override the next templates by adding your own to the directory snippets
:
snippets/form_fields.html
- renders all form widgets and block of global form errors;snippets/form_field.html
- render a single form widget.
If you do not override them, macros {{ form_fields(form) }}
will use predefined templates. Look at the sources here:
So, if you need to change look and feel of your forms you can override these templates.
If you need to change the look of only a few forms and do not want to change it globally, you can create separate templates with other names (you can use generic templates as an example). Like snippets/my_custom_form.html
and snippets/my_custom_form_field.html
, for example. And than you can define your own macro for form rendering:
Now, you can use your new macro like generic one:
If you need to use the same macro in multiple templates you can put to the separate template and than import where you need it:
Last updated