API Docs for: 1.0.0
Show:

FormManager Class

Required Markup Structure

Each element (or tighly coupled set of elements) must be contained by an element that has the CSS class formmgr-row. Within each row, validation messages are displayed inside the container with CSS class formmgr-message-text.

When a message is displayed inside a row, the CSS class formmgr-has{type} is placed on the row container and the containing fieldset (if any), where {type} is the message type passed to displayMessage().

Initializing the Form

Default values can be either encoded in the markup or passed to the FormManager constructor via config.default_value_map. (The former method is obviously better for progressive enhancement.) The values passed to the constructor override the values encoded in the markup.

prepareForm() must be called before the form is displayed. To initialize focus to the first element in a form, call initFocus(). If the form is in an overlay, you can delay these calls until just before showing the overlay.

The default values passed to the constructor are inserted by populateForm(). (This is automatically called by prepareForm().)

Hidden fields are ignored. If you want FormManager to process a value that is invisible to the user, use a normal input with display:none;.

Displaying Messages

To display a message for a single form row, call displayMessage(). To display a message for the form in general, call displayFormMessage(). These functions can be used for initializing the error display when the page loads, for displaying the results of pre-validation, and for displaying the results of submitting a form via XHR.

Specifying Validations

The following classes can be applied to a form element for pre-validation:

yiv-required
Value must not be empty.
yiv-length:[x,y]
String must be at least x characters and at most y characters. At least one of x and y must be specified.
yiv-integer:[x,y]
The integer value must be at least x and at most y. x and y are both optional.
yiv-decimal:[x,y]
The decimal value must be at least x and at most y. Exponents are not allowed. x and y are both optional.

If we ever need to allow exponents, we can use yiv-float.

The following functions allow additional pre-validation to be attached to individual form elements:

setRegex()
Sets the regular expression that must match in order for the value to be acceptable.
setFunction()
Sets the function that must return true in order for the value to be acceptable. The function is called in the scope of the Form object with the arguments: the form and the element.

setErrorMessages() specifies the error message to be displayed when a pre-validation check fails.

Functions are expected to call displayMessage() directly.

More complex pre-validations can be added by overriding postValidateForm(), described below.

Validation normally strips leading and trailing whitespace from every value. If you have a special case where this should not be done, add the CSS class yiv-no-trim to the input field.

Derived classes may also override the following functions:

prePrepareForm(arguments passed to prepareForm)
Called before filling in default values for the form elements. Return false to cancel dialog.
postPrepareForm(arguments passed to prepareForm)
Called after filling in default values for the form elements.
postValidateForm(form)
Called after performing the basic pre-validations. Returns true if the form contents are acceptable. Reports error if there is a problem.

Constructor

FormManager

(
  • form_name
  • config
)

Parameters:

  • form_name String

    The name attribute of the HTML form.

  • config Object

    Configuration. status_node is an optional element in which to display overall status. default_value_map is an optional mapping of form element names to default values. Default values encoded in the markup will be merged into this map, but values passed to the constructor will take precedence.

Methods

addErrorMessage

(
  • id
  • error_type
  • msg
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:713

Set one particular error message for a form element.

Parameters:

  • id String | Object

    The selector for the element or the element itself

  • error_type String

    The error message type. Refer to setErrorMessages() for details.

  • msg String

    The error message

cleanValues

(
  • e
)
Boolean static

Trim leading and trailing whitespace from the specified fields, except when a field has the CSS class yiv-no-trim.

Parameters:

  • e Array

    The fields to clean.

Returns:

Boolean:

true if there are any file inputs.

clearForm

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:734

Reset all values in the form to the defaults specified in the markup.

clearMessage

(
  • e
)
static

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:451

Exposed for use by Y.QueryBuilder

Clear the message for the given field.

Parameters:

  • e Element | Node

    the field

clearMessages

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1089

Clear all messages in status_node and the form rows.

disableForm

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1019

Disable all the registered buttons.

displayFormMessage

(
  • msg
  • error
  • scroll
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1163

Display a message in status_node.

Parameters:

  • msg String

    The message

  • error Boolean

    true if the message is an error

  • scroll Boolean

    true if status_node should be scrolled into view

displayMessage

(
  • e
  • msg
  • type
  • [scroll]
)
Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1117

Display a message for the form row containing the specified element. The message will only be displayed if no message with a higher precedence is already visible. (see Y.FormManager.status_order)

Parameters:

  • e String | Object

    The selector for the element or the element itself

  • msg String

    The message

  • type String

    The message type (see Y.FormManager.status_order)

  • [scroll] Boolean optional

    true if the form row should be scrolled into view

Returns:

Boolean:

true if the message was displayed, false if a higher precedence message was already there

displayMessage

(
  • e
  • msg
  • type
  • [had_messages]
  • [scroll]
)
Boolean static

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:472

Exposed for use by Y.QueryBuilder

Display a message for the form row containing the specified element. The message will only be displayed if no message with a higher precedence is already visible. (see Y.FormManager.status_order)

Parameters:

  • e String | Object

    The selector for the element or the element itself

  • msg String

    The message

  • type String

    The message type (see Y.FormManager.status_order)

  • [had_messages] Boolean optional

    true if the form already has messages displayed

  • [scroll] Boolean optional

    true if the form row should be scrolled into view

Returns:

Boolean:

true if the message was displayed, false if a higher precedence message was already there

enableForm

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1009

Enable all the registered buttons.

getChanges

() Object

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:792

Return the modified values.

Returns:

Object:

map of form element names to new values

getElementStatus

(
  • e
)
Mixed static

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:273

Get the status of the given fieldset or form row.

Parameters:

  • e String | Object

    The descriptor or DOM element.

Returns:

Mixed:

The status (String) or false.

getForm

() DOM

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:549

Returns:

DOM:

The form DOM element.

getRowStatus

(
  • e
)
Mixed

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1075

Get the message type displayed for the row containing the specified element.

Parameters:

  • e String | Object

    The selector for the element or the element itself

Returns:

Mixed:

The status (String) or false.

getStatusPrecedence

(
  • status
)
Int static

Get the precedence of the given status name.

Parameters:

  • status String

    The name of the status value.

Returns:

Int:

The position in the status_order array.

hasErrors

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1066

Returns:

Boolean:

true if there are any error messages displayed

hasFileInputs

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:562

Returns:

Boolean:

true if the form contains file inputs. These require special treatment when submitting via XHR.

hasMessages

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1057

Returns:

Boolean:

true if there are any messages displayed, of any type

initFocus

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:857

Set focus to first input field. If a page contains multiple forms, only call this for one of them.

isChanged

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:773

Check if form values have been modified.

Returns:

Boolean:

false if all form elements have the default values passed to the constructor

isFormEnabled

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1000

Returns:

Boolean:

true if form is enabled

notifyErrors

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1151

Displays a generic message in status_node stating that the form data failed to validate. Override this if you want to get fancy.

populateForm

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:746

Reset all values in the form to the defaults passed to the constructor or to setDefaultValues().

postPopulateForm

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:763

Hook for performing additional actions after populateForm() completes.

postPrepareForm

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:846

Hook called after prepareForm() executes.

Returns:

Boolean:

Return value from this function is returned by prepareForm().

postValidateForm

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:963

Hook called at the end of validateForm(). This is the best place to put holistic validations that touch multiple form elements.

Returns:

Boolean:

false if validation fails

prepareForm

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:813

Prepare the form for display.

Returns:

Boolean:

true if both pre & post hooks are happy

prePrepareForm

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:835

Hook called before prepareForm() executes.

Returns:

Boolean:

false cancels prepareForm().

registerButton

(
  • el
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:980

Register an object that can be disabled. The object must support the set('disabled', ...) API. (The exception is DOM nodes, since they are automatically wrapped in Y.Node.) Buttons contained within the form DOM element are automatically registered.

Parameters:

  • el String | Object

    The selector for the element or the element itself

saveCurrentValuesAsDefault

()

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:612

Store the current form values in default_value_map.

setDefaultValue

(
  • field_name
  • default_value
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:598

Set the default values for a single form element.

Parameters:

  • field_name String

    The form element name.

  • default_value String | Int | Float

    The default value.

setDefaultValues

(
  • default_value_map
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:581

Set the default values for all form elements.

Parameters:

  • default_value_map Object | Model

    Mapping of form element names to values.

setErrorMessages

(
  • id
  • map
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:682

Set the error messages for a form element. This can be used to override the default messages for individual elements

The valid error types are:

required
 
min_length
{min} and {max} are replaced
max_length
{min} and {max} are replaced
integer
{min} and {max} are replaced
decimal
{min} and {max} are replaced
regex
This must be set for elements which validate with regular expressions.

Parameters:

  • id String | Object

    The selector for the element or the element itself

  • map Object

    Map of error types to error messages.

setFormEnabled

(
  • enabled
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:1029

Set the enabled state all the registered buttons.

Parameters:

  • enabled Boolean

    true to enable the form, false to disable the form

setFunction

(
  • id
  • f
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:628

Set the validation function for a form element.

Parameters:

  • id String | Object

    The selector for the element or the element itself

  • f Function | String | Object

    The function to call after basic validations succeed. If this is a String, it is resolved in the scope of the FormManager object. If this is an object, it must be {fn:, scope:}. The function will then be invoked in the specified scope.

setRegex

(
  • id
  • regex
  • flags
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:647

Set the regular expression used to validate the field value.

Since there is no default message for failed regular expression validation, this function will complain if you have not already called setErrorMessages() or addErrorMessage to specify an error message.

Parameters:

  • id String | Object

    The selector for the element or the element itself

  • regex String | RegExp

    The regular expression to use

  • flags String

    If regex is a String, these are the flags used to construct a RegExp.

setStatusNode

(
  • node
)

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:571

Parameters:

  • node String | Y.Node

    the node in which status should be displayed

statusTakesPrecedence

(
  • orig_status
  • new_status
)
Boolean static

Compare two status values.

Parameters:

  • orig_status String

    The name of the original status value.

  • new_status String

    The name of the new status value.

Returns:

Boolean:

true if new_status takes precedence over orig_status

validateForm

() Boolean

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:887

Returns:

Boolean:

true if all validation checks passed

validateFromCSSData

(
  • e
  • [msg_list]
)
Object static

Validate an input based on its CSS data.

Parameters:

  • e Element | Node

    The field to validate.

  • [msg_list] Map optional

    Map of message types to custom messages.

Returns:

Object:

Status

keepGoing
(Boolean) true if further validation should be done.
error
(String) The error message, if any.

Properties

decimal_value_re

RegExp static

Regular expression used to determine if a value is a decimal number. This can be localized, e.g., use the correct decimal separator.

field_marker_class

String

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:183

The CSS class which marks each field in a row of the form. This enables messaging when multiple fields are in a single row.

integer_value_re

RegExp static

Regular expression used to determine if a value is an integer. This can be localized, e.g., allow for thousands separator.

row_marker_class

String

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:174

The CSS class which marks each row of the form. Typically, each field (or a very tightly coupled set of fields) is placed in a separate row.

row_status_prefix

String

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:229

The prefix for all CSS classes placed on a form row when pre-validation fails. The full CSS class is formed by appending the value from Y.FormManager.status_order.

status_failure_class

String

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:219

The CSS class placed on status_node when displayFormMessage() is called with error=true.

status_marker_class

String

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:192

The CSS class which marks the container for the status message within a row of the form.

status_none_class

String

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:201

The CSS class placed on status_node when it is empty.

status_order

Array static

Names of supported status values, highest precedence first.

This is static because it links to CSS rules that define the appearance of each status type: .formmgr-has{status}

Default: [ 'error', 'warn', 'success', 'info' ]

status_success_class

String

Provided by the gallery-formmgr module.

Defined in src/gallery-formmgr/js/FormManager.js:209

The CSS class placed on status_node when displayFormMessage() is called with error=false.

Strings

Object static

Map of localizable strings used by pre-validation.

validation_error
Displayed in status_node by notifyErrors() when pre-validation fails.
required_string
Displayed when yiv-required fails on an input field.
required_menu
Displayed when yiv-required fails on a select element.
length_too_short, length_too_long, length_out_of_range
Displayed when yiv-length fails on an input field.
integer, integer_too_small, integer_too_large, integer_out_of_range
Displayed when yiv-integer fails on an input field.
decimal, decimal_too_small, decimal_too_large, decimal_out_of_range
Displayed when yiv-decimal fails on an input field.