2016-04-07 22:21:01 +09:00
# Nova Forms
2016-03-30 19:08:06 +09:00
2016-04-04 11:30:14 +09:00
This package provides a `NovaForm` component that works with the schema extension defined in the [smart-methods ](https://github.com/meteor-utilities/smart-methods ) package to let you easily generate new document and edit document forms.
2016-03-30 19:08:06 +09:00
### Install
2016-04-04 11:30:14 +09:00
`meteor add nova:forms`
2016-03-30 19:08:06 +09:00
2016-04-07 15:53:18 +09:00
(note: package is not published yet)
### Features
This package can generate new document and edit document forms from a [SimpleSchema ](https://github.com/aldeed/meteor-simple-schema ) schema. Features include:
- Error handling.
- Bootstrap-compatible.
- Cross-component communciation (prefill a field based on another).
- Callbacks on form submission, success, and failure.
- Support for basic form controls (input, textarea, radio, etc.).
- Support for custom form controls.
- Submission to Meteor methods.
### Usage
2016-04-07 16:05:40 +09:00
Example schema:
```js
import BodyFormControl from './components/BodyFormControl.jsx';
const isLoggedIn = (user) => !!user;
const isOwner = (user, document) => user._id === document.userId;
const isAdmin = (user) => user.isAdmin;
const PostsSchema = new SimpleSchema({
postedAt: {
type: Date,
optional: true
// no insertableIf or editableIf means this field won't appear in forms
},
title: {
type: String,
optional: false,
max: 500,
insertableIf: isLoggedIn,
editableIf: isOwner,
control: "text",
order: 1
},
body: {
type: String,
optional: true,
max: 3000,
insertableIf: isLoggedIn,
editableIf: isOwner,
control: BodyFormControl,
order: 2
},
sticky: {
type: Boolean,
optional: true,
defaultValue: false,
insertableIf: isAdmin,
editableIf: isAdmin,
control: "checkbox",
order: 3
},
}
```
2016-04-07 15:53:18 +09:00
New document form:
```jsx
< NovaForm
collection={Posts}
currentUser={currentUser},
methodName="posts.new"
/>
```
Edit document form:
```jsx
< NovaForm
collection={Posts}
currentUser={currentUser},
methodName="posts.edit",
document={post},
labelFunction={capitalize}
successCallback={closeModal}
/>
```
2016-04-04 11:30:14 +09:00
### Props
2016-03-30 19:08:06 +09:00
2016-04-07 15:53:18 +09:00
###### `collection`
2016-03-30 19:08:06 +09:00
2016-04-04 11:30:14 +09:00
The collection in which to edit or insert a document.
2016-03-30 19:08:06 +09:00
2016-04-07 15:53:18 +09:00
###### `document`
2016-03-30 19:08:06 +09:00
2016-04-04 11:30:14 +09:00
If present, the document to edit. If not present, the form will be a “new document” form.
2016-03-30 19:08:06 +09:00
2016-04-07 15:53:18 +09:00
###### `currentUser`
2016-04-04 11:30:14 +09:00
The current user.
2016-04-07 15:53:18 +09:00
###### `submitCallback()`
2016-04-04 11:30:14 +09:00
A callback called on form submission.
2016-04-07 15:53:18 +09:00
###### `successCallback(document)`
2016-04-04 11:30:14 +09:00
A callback called on method success.
2016-04-07 15:53:18 +09:00
###### `errorCallback(document, error)`
2016-04-04 11:30:14 +09:00
A callback called on method failure.
2016-04-08 10:29:32 +09:00
###### `cancelCallback()`
If provided, will show a "cancel" link next to the form's submit button.
2016-04-07 15:53:18 +09:00
###### `methodName`
2016-04-04 11:30:14 +09:00
The name of the Meteor method to call.
2016-04-07 15:53:18 +09:00
###### `labelFunction`
2016-04-04 11:30:14 +09:00
A function to call on field names to get the label.
2016-04-07 15:53:18 +09:00
###### `prefilledProps`
2016-04-04 11:30:14 +09:00
2016-04-04 16:58:53 +09:00
A set of props to prefill for new documents.
### Collection Schema
This package generates forms based on the following special schema properties (see also the [Smart Methods ](https://github.com/meteor-utilities/smart-methods ) package:
2016-04-07 15:53:18 +09:00
###### `insertableIf(user)`
2016-04-04 16:58:53 +09:00
A function called on the `user` performing the operation, should return `true` or `false` . When generating a form for inserting new documents, the form will contain all the fields that return `true` for the current user.
2016-04-07 15:53:18 +09:00
###### `editableIf(user, document)`
2016-04-04 16:58:53 +09:00
Called on the `user` performing the operation, and the `document` being operated on, and should return `true` or `false` . When generating a form for editing existing documents, the form will contain all the fields that return `true` for the current user.
2016-04-07 15:53:18 +09:00
###### `control`
2016-04-04 16:58:53 +09:00
Either a text string (one of `text` , `textarea` , `checkbox` , `checkboxgroup` , `radiogroup` , or `select` ) or a React component.
2016-04-07 15:53:18 +09:00
###### `order`
2016-04-04 16:58:53 +09:00
A number corresponding to the position of the property's field inside the form.
### Context
The main `NovaForm` components makes the following objects available as context to all its children:
2016-04-07 15:53:18 +09:00
###### `prefilledValues`
2016-04-07 14:20:44 +09:00
An object containing optional prefilled properties.
2016-04-07 15:53:18 +09:00
###### `addToPrefilledValues({name: value})`
2016-04-07 14:20:44 +09:00
A function that takes a property, and adds it to the `prefilledValues` object.
2016-04-07 15:53:18 +09:00
###### `throwError({content, type})`
2016-04-04 16:58:53 +09:00
2016-04-07 14:20:44 +09:00
A callback function that can be used to throw an error.