cerebral-legacy / cerebral-module-forms

Form handling for Cerebral
http://cerebral-website.herokuapp.com/documentation/cerebral-module-forms
MIT License
12 stars 5 forks source link

Refactor of forms and updated readme #38

Closed christianalfoni closed 8 years ago

christianalfoni commented 8 years ago

Okay, so I got inspired and did a complete refactor of Forms. Instead of explaining what I removed, I will rather explain now how it works. I have also rewritten the docs for the website (released soon), so they just need some tweaking after this pull gets merged.

fieldChanged({
  field: 'app.myForm.someField',
  value: e.target.value,
  touched: true
})

NOTE! No more preventValidation here, it is just a default signal for common use case. Look further

import resetForm from 'cerebral-module-forms/factories/resetForm'
import shouldValidate from 'cerebral-module-forms/factories/shouldValidate'
import touchField from 'cerebral-module-forms/factories/touchField'
import validateField from 'cerebral-module-forms/factories/validateField'
import validateForm from 'cerebral-module-forms/factories/validateForm'
import validateHasValue from 'cerebral-module-forms/factories/validateHasValue'

export default [
  // Resets to default values
  resetForm('some.form'),

  // Outputs true/false if the field has validations and a value
  shouldValidate('some.form.field'), {
    true: [],
    false: []
  },

  // Touches a field, passing true/false
  touchField('some.form.field', true),

  // Runs validation on the field
  validateField('some.form.field'),

  // Runs validation on the whole form
  validateForm('some.form'),

  // Validates if field has a value
  validateHasValue('some.form.field'),
]

This gives you complete control of how validation should work and you can easily create a chain factory to do validation the way you want to.

import getFormFields from 'cerebral-module-forms/helpers/getFormFields'
import getInvalidFormFields from 'cerebral-module-forms/helpers/getInvalidFormFields'
import isValidForm from 'cerebral-module-forms/helpers/isValidForm'
import toJSON from 'cerebral-module-forms/helpers/toJSON'

// Gives you an object of {'some.path':  {isValid: true...}} of all fields,
// also nested and arrays. Can be used to check whatever by iterating its keys.
// Used by isValidForm and getInvalidFormFields
getFormFields(someForm)

// Same as above, though only invalid form fields
getInvalidFormFields(someForm)

// Returns true if all fields are valid
isValidForm(someForm)

// Show form as a nested object (if nested forms) with only values
toJSON(someForm) // {name: 'hey', address: {street: 'some street'}}

This pull has breaking changes and requires a new version, but now I feel that everything is under control and a lot more composeable

schotime commented 8 years ago

Is there anyway the debugger can put some more information about the field when it gets into the debugger?

eg. fieldChanged - simple.email.value (10) (sync)

image

christianalfoni commented 8 years ago

Hold on with the merge, have a couple of things to fix :)

christianalfoni commented 8 years ago

@schotime More info in what way? :)

schotime commented 8 years ago

@christianalfoni Just to maybe display the form that the signal belongs to in this case.

christianalfoni commented 8 years ago

@schotime Ah I see... well, the signal does not really belong to a specific form. It is just a signal registered on the forms module to do changes to whatever fields wherever in your state tree, based on the payload you give it :) So the state changes there and the input is the only details we have on what it does, the signal itself is completely generic.

But new docs all this becomes much clearer.