Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: add form controller #15

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

wip: add form controller #15

wants to merge 7 commits into from

Conversation

43081j
Copy link
Owner

@43081j 43081j commented Apr 3, 2023

playing around with introducing a form controller.

with this kind of usage:

const initialValue = {foo: 'bar'};
const ctrl = new FormController(this, initialValue);

html`
  <form ${ctrl.attach()}>
    <label>
      Email:
      <input ${ctrl.bind('email')}>
    </label>
    <label>
      Password:
      <input type="password" ${ctrl.bind('password')}>
    </label>
  </form>
`;

you can also use validators:

  • form validators run on submission after user input has updated the model
  • field validators run on user input before user input updates the model (and can prevent it)
ctrl.addValidator((values) => {
  // values here is `ctrl.value`
  // this will be called on submission and decide if the form should submit or not
});

ctrl.addValidator(('email', (value) => {
  // value here is the email from `ctrll.value.email`
  // returning a string here will imply the field is invalid and will add the string to `ctrl.errors`
  // returning void or null will imply the field is valid
});

draft until it becomes less of an endless what if?

@coveralls
Copy link

coveralls commented Apr 3, 2023

Pull Request Test Coverage Report for Build 4660014813

  • 251 of 251 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 98.565%

Totals Coverage Status
Change from base Build 4420305954: 0.3%
Covered Lines: 2326
Relevant Lines: 2345

💛 - Coveralls

@43081j
Copy link
Owner Author

43081j commented Apr 6, 2023

we should add:

  • validation of some sort
  • automatic handling of submissions (just listen to the submit event and expose some kind of submit handler, but also provide some kind of endpoint for the common cases)
  • expose validation errors on ctrl.errors

@43081j
Copy link
Owner Author

43081j commented Apr 9, 2023

still perfecting this but now we're getting somewhere:

// snipped - see next post

to figure out:

basically the form validators don't work right now. they get passed the entire object but before any changes from the user input happened (making them useless).

validators are meant to prevent input from updating the value if they return falsely.

so either we throw away 'form validators' and stick with just field validators, or we create a deep clone of the value just to give these validators (so they can still prevent changes).

not sure yet...

@43081j
Copy link
Owner Author

43081j commented Apr 10, 2023

ok new update:

  • form validators run on submission after user input has updated the model
  • field validators run on user input before user input updates the model (and can prevent it)

so updated usage:

ctrl.addValidator((values) => {
  // values here is `ctrl.value`
  // this will be called on submission and decide if the form should submit or not
});

ctrl.addValidator(('email', (value) => {
  // value here is the email from `ctrll.value.email`
  // returning a string here will imply the field is invalid and will add the string to `ctrl.errors`
  // returning void or null will imply the field is valid
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants