forms.core

->Form

(->Form state-atom validator opts)

constructor

(constructor validator)(constructor validator data)(constructor validator data opts)

Form constructor. It accepts the following arguments:

  • validator - returned either by the form.validator/validator or form.validator/comp-validators function
  • data - initial data map
  • opts - map with the form options:
    • :on-commit - function to be called when the form is commited (by calling (commit! form))
    • :auto-validate? - should the form be validated on any data change

errors-keypaths

(errors-keypaths data)(errors-keypaths data path results)

Calculates the error key paths from the error map. It is used to mark all invalid key paths as dirty

Form

IForm

protocol

IForm protocol defines the form behavior

members

clear-cached-dirty-key-paths!

(clear-cached-dirty-key-paths! this)

Clear dirty key paths that are cached after caling the mark-dirty! function

data

(data this)

Returns the atom that holds the current data map.

errors

(errors this)

Returns the atom that holds the current error map. This map holds all errors, regardless of the key path dirty state.

validate!

(validate! this)(validate! this dirty-only?)

Validates the form.

When called without the second argument - (validate! form) it will validate the whole form and mark all invalid fields as dirty This should happen on submit so you can show all form errors to the user.

When called with true as the second argument - (validate! form true) it wil validate only the fields that are marked as dirty - fields that are not nil and that have a different value than in the :init-data. This can be called on change or blur events.

init!

(init! this)

Initializes the form. If the form constructor was called with the auto-validate? option set to true it will add a watch to the internal state atom and validate the form every time data was changed

dirty-paths-valid?

(dirty-paths-valid? this)

Are the dirty key paths in the valid state

is-valid-path?

(is-valid-path? this key-path)

Is the key path in the valid state

mark-dirty-paths!

(mark-dirty-paths! this)

Creates a diff between the initial data and the current data. Based on that diff it marks the key paths that are dirty.

commit!

(commit! this)

Commits the form. It will validate the form and mark dirty key paths. After that it will call the :on-commit function that can be used to persist the form.

mark-dirty!

(mark-dirty! this)

Mark all invalid key paths as dirty. It will validate the whole form and cache any key paths that have errors.

errors-for-path

(errors-for-path this key-path)

Returns errors for the key path. It is possible that this function will return nil even though the errors exist in the :errors map because this function accounts for the key path dirty state. If the field has errors, but it’s not dirty this function will return nil

data-for-path

(data-for-path this key-path)

Returns data for the key path

state

(state this)

Returns inner state atom. The state map holds the following properties:

  • :errors - map of the current form errors
  • :init-data - initial form data that was passed to the constructor
  • :data - current form data
  • :cached-dirty-key-paths - set of the key paths that were dirty when the whole form was validated
  • :dirty-key-paths - set of the dirty key paths

reset-form!

(reset-form! this)(reset-form! this init-data)

Reset form to the initial state

is-valid?

(is-valid? this)

Is the form in the valid state

update!

(update! this data)

Updates the data, marks dirty key paths and validates the form

map->Form

(map->Form G__10492)