Skip to content

Latest commit

 

History

History
933 lines (436 loc) · 27.7 KB

api.md

File metadata and controls

933 lines (436 loc) · 27.7 KB

aldeed:autoform Public API

Easily create forms with automatic insert and update, and automatic reactive validation.

API documentation automatically generated by docmeteor.

AutoForm.Utility {any}  Client

This property Utility is defined in AutoForm

AutoForm.Utility = Utility; autoform-api.js:11

AutoForm.addHooks(formIds, hooks)  Client

This method addHooks is defined in AutoForm

Arguments

Form id or array of form IDs to which these hooks apply. Specify null to add hooks that will run for every form.

  • hooks {Object}

Hooks to add, where supported names are "before", "after", "formToDoc", "docToForm", "onSubmit", "onSuccess", and "onError".

Returns {undefined}

Defines hooks to be used by one or more forms. Extends hooks lists if called multiple times for the same form.

AutoForm.addHooks = function autoFormAddHooks(formIds, hooks, replace) { ... autoform-api.js:23

AutoForm.hooks(hooks)  Client

This method hooks is defined in AutoForm

Arguments

  • hooks {Object}

Returns {undefined}

Defines hooks by form id. Extends hooks lists if called multiple times for the same form.

AutoForm.hooks = function autoFormHooks(hooks, replace) { ... autoform-api.js:51

AutoForm._hooks {any}  Client

This property _hooks is defined in AutoForm

Hooks list to aid automated testing

AutoForm._hooks = Hooks.form; autoform-api.js:63

AutoForm._globalHooks {any}  Client

This property _globalHooks is defined in AutoForm

Global hooks list to aid automated testing

AutoForm._globalHooks = Hooks.global; autoform-api.js:71

AutoForm._forceResetFormValues(formId)  Client

This method _forceResetFormValues is defined in AutoForm

Arguments

  • formId {String}

Returns {undefined}

Forces an AutoForm's values to properly update. See https:

AutoForm._forceResetFormValues = function autoFormForceResetFormValues(formId) { ... autoform-api.js:82

AutoForm.resetForm(formId, [template])  Client

This method resetForm is defined in AutoForm

Arguments

Looked up if not provided. Pass in for efficiency.

Returns {undefined}

Resets an autoform, including resetting validation errors. The same as clicking the reset button for an autoform.

AutoForm.resetForm = function autoFormResetForm(formId, template) { ... autoform-api.js:100

AutoForm.setDefaultTemplate(template)  Client

This method setDefaultTemplate is defined in AutoForm

Arguments

  • template {String}

AutoForm.setDefaultTemplate = function autoFormSetDefaultTemplate(template) { ... autoform-api.js:111

AutoForm.getDefaultTemplate()  Client

This method getDefaultTemplate is defined in AutoForm

Reactive.

AutoForm.getDefaultTemplate = function autoFormGetDefaultTemplate() { ... autoform-api.js:122

AutoForm.setDefaultTemplateForType(type, template)  Client

This method setDefaultTemplateForType is defined in AutoForm

Arguments

  • type {String}
  • template {String}

AutoForm.setDefaultTemplateForType = function autoFormSetDefaultTemplateForType(type, template) { ... autoform-api.js:133

AutoForm.getDefaultTemplateForType(type)  Client

This method getDefaultTemplateForType is defined in AutoForm

Arguments

  • type {String}

Returns {String} Template name

Reactive.

AutoForm.getDefaultTemplateForType = function autoFormGetDefaultTemplateForType(type) { ... autoform-api.js:152

AutoForm.getTemplateName(templateType, templateName, [fieldName], [skipExistsCheck])  Client

This method getTemplateName is defined in AutoForm

Arguments

  • templateType {String}
  • templateName {String}
  • fieldName {String} (Optional)
  • skipExistsCheck {Boolean} (Optional)

Pass true to return a template name even if that template hasn't been defined.

Returns {String} Template name

Returns the full template name. In the simplest scenario, this is templateType_templateName as passed in. However, if templateName is not provided, it is looked up in the following manner:

  1. autoform..template from the schema (field+type override for all forms)
  2. autoform.template from the schema (field override for all forms)
  3. template- attribute on an ancestor component within the same form (form+type for all fields)
  4. template attribute on an ancestor component within the same form (form specificity for all types and fields)
  5. Default template for component type, as set by AutoForm.setDefaultTemplateForType
  6. Default template, as set by AutoForm.setDefaultTemplate.
  7. Built-in default template, currently bootstrap-3.

AutoForm.getTemplateName = function autoFormGetTemplateName(templateType, templateName, fieldName, skipExistsCheck) { ... autoform-api.js:181

AutoForm.getFormValues(formId, [template], [ss], [getModifier])  Client

This method getFormValues is defined in AutoForm

Arguments

  • formId {String}

The id attribute of the autoForm you want current values for.

The template instance, if already known, as a performance optimization.

The SimpleSchema instance, if already known, as a performance optimization.

  • getModifier {Boolean} (Optional)

Set to true to return a modifier object or false to return a normal object. For backwards compatibility, and object containing both is returned if this is undefined.

Returns {Object|null}

Returns an object representing the current values of all schema-based fields in the form. The returned object is either a normal object or a MongoDB modifier, based on the getModifier argument. Return value may be null if the form is not currently rendered on screen.

AutoForm.getFormValues = function autoFormGetFormValues(formId, template, ss, getModifier) { ... autoform-api.js:255

AutoForm.getFieldValue(fieldName, [formId])  Client

This method getFieldValue is defined in AutoForm

Arguments

  • fieldName {String}

The name of the field for which you want the current value.

  • formId {String} (Optional)

The id attribute of the autoForm you want current values for. Default is the closest form from the current context.

Returns {Any|undefined}

Returns the value of the field (the value that would be used if the form were submitted right now). This is a reactive method that will rerun whenever the current value of the requested field changes. Return value will be undefined if the field is not currently rendered.

AutoForm.getFieldValue = function autoFormGetFieldValue(fieldName, formId) { ... autoform-api.js:409

AutoForm.getInputTypeTemplateNameForElement(element)  Client

This method getInputTypeTemplateNameForElement is defined in AutoForm

Arguments

The input DOM element, generated by an autoform input control

Returns {String}

Returns the name of the template used to render the element.

AutoForm.getInputTypeTemplateNameForElement = function autoFormGetInputTypeTemplateNameForElement(element) { ... autoform-api.js:444

AutoForm.getInputValue(element, [ss])  Client

This method getInputValue is defined in AutoForm

Arguments

The input DOM element, generated by an autoform input control, which must have a data-schema-key attribute set to the correct schema key name.

Provide the SimpleSchema instance if you already have it.

Returns {Any}

Returns the value of the field (the value that would be used if the form were submitted right now). Unlike AutoForm.getFieldValue, this function is not reactive.

AutoForm.getInputValue = function autoFormGetInputValue(element, ss) { ... autoform-api.js:472

AutoForm.addInputType(name, definition)  Client

This method addInputType is defined in AutoForm

Arguments

  • name {String}

The type string that this definition is for.

  • definition {Object}

Defines how the input type should be rendered.

* __componentName__ *{String}*  

The component name. A template with the name <componentName>_bootstrap3, and potentially others, must be defined.

Returns {undefined}

Use this method to add custom input components.

AutoForm.addInputType = function afAddInputType(name, definition) { ... autoform-api.js:565

AutoForm.addFormType(name, definition)  Client

This method addFormType is defined in AutoForm

Arguments

  • name {String}

The type string that this definition is for.

  • definition {Object}

Defines how the submit type should work

* __adjustInputContext__ *{Function}*  (Optional)

A function that accepts a single argument, which is the context with which an input template in the form will be called, potentially changes the context object, and then returns it. For example, the "readonly" and "disabled" form types use this function to add the "readonly" or "disabled" attribute, respectively, to every input within the form.

* __adjustSchema__ *{Function}*  (Optional)

A function that accepts a single argument, which is the form schema, and potentially uses that to return a different schema to use instead. For example, the "update-pushArray" form type uses this function to build and return a schema that is limited by the `scope` attribute on the form. When this function is called, `this` contains useful information about the form.

* __hideArrayItemButtons__ *{Boolean}*  (Optional)

Set to `true` if this form type should not show buttons for adding and removing items in an array field. The "disabled" and "readonly" form types do this.

* __onSubmit__ *{Function}*  

A function that does whatever should happen upon submission of this form type. When this function is called, `this` contains useful information about the form. At a minimum, you probably want to call `this.event.preventDefault()` to prevent the browser from submitting the form. Your submission logic may want to rely on additional custom form attributes, which will be available in `this.formAttributes`. If you do any additional validation and it fails, you should call `this.failedValidation()`. When your logic is done, you should call `this.result(error, result)`. If you want to end the submission process without providing a result, call `this.endSubmission()`. If you don't call `this.result()` or `this.endSubmission()`, `endSubmit` hooks won't be called, so for example the submit button might remain disabled. `onError` hooks will be called only if you pass an error to `this.result()`. `onSuccess` hooks will be called only if you do not pass an error to `this.result()`.

* __shouldPrevalidate__ *{Function}*  (Optional)

A function that returns `true` if validation against the form schema should happen before the `onSubmit` function is called, or `false` if not. When this function is called, `this` contains useful information about the form. If this function is not provided for a form type, the default is `true`.

* __validateForm__ *{Function}*  

A function that validates the form and returns `true` if valid or `false` if not. This can happen during submission but also at other times. When this function is called, `this` contains useful information about the form and the validation options.

Returns {undefined}

Use this method to add custom form types.

AutoForm.addFormType = function afAddFormType(name, definition) { ... autoform-api.js:586

AutoForm.validateField(formId, fieldName, [skipEmpty])  Client

This method validateField is defined in AutoForm

Arguments

  • formId {String}

The id attribute of the autoForm you want to validate.

  • fieldName {String}

The name of the field within the autoForm you want to validate.

  • skipEmpty {Boolean} (Optional, Default = false)

Set to true to skip validation if the field has no value. Useful for preventing required errors in form fields that the user has not yet filled out.

Returns {Boolean} Is it valid?

In addition to returning a boolean that indicates whether the field is currently valid, this method causes the reactive validation messages to appear.

AutoForm.validateField = function autoFormValidateField(formId, fieldName, skipEmpty) { ... autoform-api.js:603

AutoForm.validateForm(formId)  Client

This method validateForm is defined in AutoForm

Arguments

  • formId {String}

The id attribute of the autoForm you want to validate.

Returns {Boolean} Is it valid?

In addition to returning a boolean that indicates whether the form is currently valid, this method causes the reactive validation messages to appear.

AutoForm.validateForm = function autoFormValidateForm(formId) { ... autoform-api.js:616

AutoForm.getValidationContext(formId)  Client

This method getValidationContext is defined in AutoForm

Arguments

  • formId {String}

The id attribute of the autoForm for which you want the validation context

Returns {SimpleSchemaValidationContext} The SimpleSchema validation context object.

Use this method to get the validation context, which can be used to check the current invalid fields, manually invalidate fields, etc.

AutoForm.getValidationContext = function autoFormGetValidationContext(formId) { ... autoform-api.js:652

AutoForm.findAttribute(attrName)  Client

This method findAttribute is defined in AutoForm

Arguments

  • attrName {String}

Attribute name

Returns {Any|undefined} Searches for the given attribute, looking up the parent context tree until the closest autoform is reached.

Call this method from a UI helper. Might return undefined.

AutoForm.findAttribute = function autoFormFindAttribute(attrName) { ... autoform-api.js:665

AutoForm.findAttributesWithPrefix(prefix)  Client

This method findAttributesWithPrefix is defined in AutoForm

Arguments

  • prefix {String}

Attribute prefix

Returns {Object} An object containing all of the found attributes and their values, with the prefix removed from the keys.

Call this method from a UI helper. Searches for attributes that start with the given prefix, looking up the parent context tree until the closest autoform is reached.

AutoForm.findAttributesWithPrefix = function autoFormFindAttributesWithPrefix(prefix) { ... autoform-api.js:706

AutoForm.debug()  Client

This method debug is defined in AutoForm

Call this method in client code while developing to turn on extra logging. You need to call it just one time, usually in top level client code.

AutoForm.debug = function autoFormDebug() { ... autoform-api.js:750

AutoForm.arrayTracker {any}  Client

This property arrayTracker is defined in AutoForm

AutoForm.arrayTracker = arrayTracker; autoform-api.js:764

AutoForm.getInputType(atts)  Client

This method getInputType is defined in AutoForm

Arguments

  • atts {Object}

The attributes provided to afFieldInput.

Returns {String} The input type. Most are the same as the type attributes for HTML input elements, but some are special strings that autoform interprets.

Call this method from a UI helper to get the type string for the input control.

AutoForm.getInputType = function getInputType(atts) { ... autoform-api.js:774

AutoForm.getSchemaForField(name)  Client

This method getSchemaForField is defined in AutoForm

Arguments

  • name {String}

The field name attribute / schema key.

Returns {Object|undefined}

Call this method from a UI helper to get the field definitions based on the schema used by the closest containing autoForm.

AutoForm.getSchemaForField = function autoFormGetSchemaForField(name) { ... autoform-api.js:863

AutoForm._getOptionsForField(name)  Client

This method _getOptionsForField is defined in AutoForm

Arguments

  • name {String}

The field name attribute / schema key.

Returns {Array(Object)|String|undefined}

Call this method from a UI helper to get the select options for the field. Might return the string "allowed".

AutoForm._getOptionsForField = function autoFormGetOptionsForField(name) { ... autoform-api.js:877

AutoForm.getLabelForField(name)  Client

This method getLabelForField is defined in AutoForm

Arguments

  • name {String}

The field name attribute / schema key.

Returns {Object}

Call this method from a UI helper to get the field definitions based on the schema used by the closest containing autoForm.

AutoForm.getLabelForField = function autoFormGetLabelForField(name) { ... autoform-api.js:911

AutoForm.templateInstanceForForm([formId])  Client

This method templateInstanceForForm is defined in AutoForm

Arguments

  • formId {String} (Optional)

The form's id attribute

Returns {TemplateInstance|undefined} The template instance.

Gets the template instance for the form with formId or the closest form to the current context.

AutoForm.templateInstanceForForm = function (formId) { ... autoform-api.js:930

AutoForm.viewForForm([formId])  Client

This method viewForForm is defined in AutoForm

Arguments

  • formId {String} (Optional)

The form's id attribute. Do not pass this if calling from within a form context.

Returns {Blaze.View|undefined} The Blaze.View instance for the autoForm.

Gets the Blaze.View instance for the form with formId or the closest form to the current context.

AutoForm.viewForForm = function (formId) { ... autoform-api.js:946

AutoForm.getArrayCountFromDocForField(formId, field)  Client

This method getArrayCountFromDocForField is defined in AutoForm

Arguments

  • formId {String}

The form's id attribute

  • field {String}

The field name (schema key)

Returns {Number|undefined} Array count in the attached document.

Looks in the document attached to the form to see if the requested field exists and is an array. If so, returns the length (count) of the array. Otherwise returns undefined.

AutoForm.getArrayCountFromDocForField = function (formId, field) { ... autoform-api.js:983

AutoForm.parseData(data)  Client

This method parseData is defined in AutoForm

Arguments

  • data {Object}

Current data context for the form, or an empty object. Usually this is used from a quickForm, since the autoForm won't be rendered yet. Otherwise you should use AutoForm.getCurrentDataForForm if you can.

Returns {Object} Current data context for the form, or an empty object.

Parses and alters the current data context for a form. It will have default values added and a _resolvedSchema property that has the schema the form should use.

AutoForm.parseData = function (data) { ... autoform-api.js:1003

AutoForm.getCurrentDataForForm(formId)  Client

This method getCurrentDataForForm is defined in AutoForm

Arguments

  • formId {String}

The form's id attribute

Returns {Object} Current data context for the form, or an empty object.

Returns the current data context for a form. You can call this without a formId from within a helper and the data for the nearest containing form will be returned.

AutoForm.getCurrentDataForForm = function (formId) { ... autoform-api.js:1017

AutoForm.getCurrentDataPlusExtrasForForm()  Client

This method getCurrentDataPlusExtrasForForm is defined in AutoForm

Arguments

  • ____ {any}

{String} [formId] The form's id attribute

Returns {Object} Current data context for the form, or an empty object.

Returns the current data context for a form plus some extra properties. You can call this without a formId from within a helper and the data for the nearest containing form will be returned.

AutoForm.getCurrentDataPlusExtrasForForm = function (formId) { ... autoform-api.js:1039

AutoForm.getFormCollection(formId)  Client

This method getFormCollection is defined in AutoForm

Arguments

  • formId {String}

The form's id attribute

Returns {Mongo.Collection|undefined} The Collection instance

Gets the collection for a form from the collection attribute

AutoForm.getFormCollection = function (formId) { ... autoform-api.js:1059

AutoForm.getFormSchema(formId, [form])  Client

This method getFormSchema is defined in AutoForm

Arguments

  • formId {String}

The form's id attribute

  • form {Object} (Optional)

Pass the form data context as an optimization or if the form is not yet rendered.

Returns {SimpleSchema|undefined} The SimpleSchema instance

Gets the schema for a form, from the schema attribute if provided, or from the schema attached to the Mongo.Collection specified in the collection attribute. The form must be currently rendered.

AutoForm.getFormSchema = function (formId, form) { ... autoform-api.js:1076

AutoForm.getFormId()  Client

This method getFormId is defined in AutoForm

Returns {String} The containing form's id attribute value

Call in a helper to get the containing form's id attribute. Reactive.

AutoForm.getFormId = function () { ... autoform-api.js:1088

AutoForm.selectFirstInvalidField(formId, ss)  Client

This method selectFirstInvalidField is defined in AutoForm

Arguments

  • formId {String}

The id attribute of the form

The SimpleSchema instance that was used to create the form's validation context.

Returns {undefined}

Selects the focus the first field (in DOM order) with an error.

AutoForm.selectFirstInvalidField = function selectFirstInvalidField(formId, ss) { ... autoform-api.js:1101

AutoForm._validateFormDoc(doc, isModifier, formId, ss, form, [key])  Client

This method _validateFormDoc is defined in AutoForm

Arguments

  • doc {Object}

The document with the gathered form values to validate.

  • isModifier {Boolean}

Is doc actually a mongo modifier object?

  • formId {String}

The form id attribute

The SimpleSchema instance against which to validate.

  • form {Object}

The form context object

  • key {String} (Optional)

Optionally, a specific schema key to validate.

Returns {Boolean} Is the form valid?

If creating a form type, you will often want to call this from the validateForm function. It provides the generic form validation logic that does not typically change between form types.

AutoForm._validateFormDoc = function validateFormDoc(doc, isModifier, formId, ss, form, key) { ... autoform-api.js:1155

AutoFormvalueConverters.stringToNumber(val)  Client

This method stringToNumber is defined in AutoForm.valueConverters

Arguments

  • val {String}

A string or null or undefined.

Returns {Number|String} The string converted to a Number or the original value.

For strings, returns Number(val) unless the result is NaN. Otherwise returns val.

stringToNumber: function stringToNumber(val) { ... inputTypes/value-converters.js:141