Form API
In TechMaju, the frm object provides a wide range of methods for interacting with forms. These methods are essential for setting values, refreshing forms, handling events, and more. Below is a detailed list of commonly used methods along with their descriptions and examples.
Setting Values
Method | Description |
|---|---|
| Set the value of a field. Triggers the field change event. |
// set a single value
frm.set_value('description', 'New description')
// set multiple values at once
frm.set_value({
status: 'Open',
description: 'New description'
})
// returns a promise
frm.set_value('description', 'New description')
.then(() => {
// do something after value is set
})Refreshing and Saving
Method | Description | Example |
|---|---|---|
| Refresh the form with the latest values from the server. | |
| Trigger form save, with options for Submit, Cancel, and Update. | |
Enabling/Disabling Save
Method | Description |
|---|---|
| Enable the Save button. |
| Disable the Save button. |
if (frappe.user_roles.includes('Custom Role')) {
frm.enable_save();
} else {
frm.disable_save();
}Emailing Documents
Method | Description |
|---|---|
| Open Email dialog for this form. |
// open email dialog
frm.email_doc();
// open email dialog with some message
frm.email_doc(`Hello ${frm.doc.customer_name}`);Reloading and Refreshing Fields
Method | Description | Example |
|---|---|---|
| Reload document with latest values from the server and call | |
| Refresh a specific field and its dependencies. | |
Form State and Checks
Method | Description | Example |
|---|---|---|
| Check if form values have been changed and not saved yet. | |
| Set form as "dirty". | |
| Check if the form is new and not saved yet. | |
Setting Intro Text
Method | Description |
|---|---|
| Set intro text on the top of the form with optional color. |
if (!frm.doc.description) {
frm.set_intro('Please set the value of description', 'blue');
}
Custom Buttons
Method | Description | Example |
|---|---|---|
| Add a custom button in the inner toolbar. | |
| Change a custom button type by label. | |
| Remove a custom button by label. | |
| Remove all custom buttons from the inner toolbar. | |
DocField Properties
Method | Description |
|---|---|
| Change the DocField property of a field and refresh it. |
// change the fieldtype of description field to Text
frm.set_df_property('description', 'fieldtype', 'Text');
// set the options of the status field to only be [Open, Closed]
frm.set_df_property('status', 'options', ['Open', 'Closed'])
// set a field as mandatory
frm.set_df_property('title', 'reqd', 1)
// set a field as read only
frm.set_df_property('status', 'read_only', 1)Field Toggles
Method | Description | Example |
|---|---|---|
| Toggle a field or list of fields as read-only based on a condition. | |
| Toggle a field or list of fields as mandatory based on a condition. | |
| Show/hide a field or list of fields based on a condition. | |
Setting Queries
Method | Description |
|---|---|
| Apply filters on a Link field to show limited records to choose from. You must call |
// show only customers whose territory is set to India
frm.set_query('customer', () => {
return {
filters: {
territory: 'India'
}
}
})
// show customers whose territory is any of India, Nepal, Japan
frm.set_query('customer', () => {
return {
filters: {
territory: ['in', ['India', 'Nepal', 'Japan']]
}
}
})
// set filters for Link field item_code in
// items field which is a Child Table
frm.set_query('item_code', 'items', () => {
return {
filters: {
item_group: 'Products'
}
}
})You can also override the filter method and provide your own custom method on the server side. Just the set the query to the module path of your python method.
// change the filter method by passing a custom method
frm.set_query('fieldname', () => {
return {
query: 'dotted.path.to.custom.custom_query',
filters: {
field1: 'value1'
}
}
})# python method signature
def custom_query(doctype, txt, searchfield, start, page_len, filters):
# your logic
return filtered_listChild Table Operations
Method | Description | Example |
|---|---|---|
| Add a row with values to a Table field. | |
Server Calls
Method | Description |
|---|---|
| Call a server-side controller method with arguments. While accessing any server side method using |
class ToDo(Document):
@frappe.whitelist()
def get_linked_doc(self, throw_if_missing=False):
if not frappe.db.exists(self.reference_type, self.reference_name):
if throw_if_missing:
frappe.throw('Linked document not found')
return frappe.get_doc(self.reference_type, self.reference_name)You can call it from client using frm.call.
frm.call('get_linked_doc', { throw_if_missing: true })
.then(r => {
if (r.message) {
let linked_doc = r.message;
// do something with linked_doc
}
})Triggering Events
Method | Description |
|---|---|
| Trigger any form event explicitly. |
frappe.ui.form.on('ToDo', {
refresh(frm) {
frm.trigger('set_mandatory_fields');
},
set_mandatory_fields(frm) {
frm.toggle_reqd('priority', frm.doc.status === 'Open');
}
})Getting Selected Rows
Method | Description | Example |
|---|---|---|
| Get selected rows in Child Tables in an object where key is the table fieldname and values are row names. | |
Ignoring Linked Doctypes
Method | Description |
|---|---|
| To avoid cancellation of linked documents during cancel all, you need to set the |
frappe.ui.form.on("DocType 1", {
onload: function(frm) {
// Ignore cancellation for all linked documents of respective DocTypes.
frm.ignore_doctypes_on_cancel_all = ["DocType 2", "DocType 3"];
}
}In the above example, the system will avoid cancellation for all documents of 'DocType 2' and 'DocType 3' which are linked with document of 'DocType 1' during cancellation.