2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 17:34:04 +00:00
books/docs/client/forms.md

92 lines
2.1 KiB
Markdown
Raw Normal View History

2018-01-10 12:49:52 +00:00
# Forms
Forms are automatically created from the model (DocType)
Form objects create the controls and also handler insert and update.
Note: A single Form object can handle multiple documents.
### Example
```js
2018-01-16 06:09:17 +00:00
const Page = require('frappejs/frappe/client/view/page');
const Form = require('frappejs/frappe/client/view/form');
2018-01-10 12:49:52 +00:00
edit_page = new Page('Edit To Do');
router.add('/edit/todo/:name', edit_page);
edit_page.form = new Form({
doctype: 'ToDo',
parent: edit_page.body
});
```
## Creating
To create a new Form, you need to pass the model (DocType) and `parent` element.
Controls will be created for all the `fields` of the model that is passed along with a `Submit` button
2018-01-30 12:03:04 +00:00
## Set Document for Editing
2018-01-10 12:49:52 +00:00
To setup a form for editing, you can bind a document by calling the `use` method.
```js
edit_page.on('show', async (params) => {
let doc = await frappe.get_doc('ToDo', params.name);
edit_page.form.use(doc);
})
```
2018-01-30 12:03:04 +00:00
## Extending
You can extend the form for any DocType by passing creating a module with `_client` suffix. For example `account_client` for Account
The module can export two classes `Form` and `List` these will be used to override the forms and list for the given doctypes
Example:
```js
const BaseForm = require('frappejs/client/view/form');
class AccountForm extends BaseForm {
make() {
super.make();
// override controller event
this.controls['parent_account'].get_filters = (query) => {
return {
keywords: ["like", query],
name: ["!=", this.doc.name]
}
}
}
}
module.exports = {
Form: AccountForm
}
```
2018-01-10 12:49:52 +00:00
## New Document
To setup a form for a new document, just create a new document with the Frappe.js document helpers, and `use` it with paramter `is_new` = true
```js
2018-01-10 12:57:24 +00:00
// setup todo new
frappe.router.add('new/todo', async (params) => {
2018-01-10 12:49:52 +00:00
2018-01-10 12:57:24 +00:00
// new document
2018-02-08 12:28:51 +00:00
app.doc = await frappe.getDoc({doctype: 'ToDo'});
2018-01-10 12:49:52 +00:00
2018-01-10 12:57:24 +00:00
// set a random name
2018-02-08 12:28:51 +00:00
app.doc.setName();
2018-01-10 12:49:52 +00:00
2018-01-10 12:57:24 +00:00
// show the page
app.edit_page.show();
2018-01-10 12:49:52 +00:00
2018-01-10 12:57:24 +00:00
// is_new=true
app.edit_page.form.use(app.doc, true);
});
2018-01-10 12:49:52 +00:00
```