question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simpler way to set errors after form submission

See original GitHub issue

Not sure if it’s just me, but the process of showing errors on a form is a bit cumbersome:

  1. import { setErrors } from '@formkit/vue'
  2. Set a unique ID on your form
  3. Invoke setErrors() with your unique ID

What if the form submission handler was passed a second parameter containing a node object?

function submit(value, node) {
  node.setErrors(["Username not available."])
} 

Implementation details free to be changed but something like this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
justin-schroedercommented, Feb 21, 2022

@tosling solid points — one thing to note that will help with the unique ID thing is FormKit already produces unique IDs for you — you technically dont need to specify it yourself — but only explicitly specified ones end up in the global getNode() registry.

For submitting forms, which is now in the beta.3 branch as of a few minutes ago, you can submit these forms 3 ways:

// any id works, even the auto-generated ones
submitForm('formId')
$formkit.submit('formId')
node.submit()

An alternative way to get a node is using the @node event which is emitted from all <FormKit> components with the node as a payload. And in beta.3 you can also get the node via template ref:

<template>
  <FormKit ref="foobar" />
</template>

<script>
export default {
  mounted () {
    this.$refs.foobar.node.input('hello world')
  }
}
</script>

Playground example

1reaction
justin-schroedercommented, Feb 15, 2022

I think @huzaifahj’s suggestion in great, and plan to implement it or something similar! I’m curious about your opposition to using id to reference core nodes @tosling. Our rational for ID vs template refs:

  • It’s already useful in native JS and allows native js compatible code
  • It insures unique identification
  • It does not require importing ref to use it
  • It can be used globally instead of template refs that can only be used in the bound context where the template was defined
  • provides a consistent API between schema and template based implementations
  • no need to artificially surface internals of the FormKit component which leads to quick misuse of the internals

That said, the DX still has a ways to go in my opinion. On the note of DX — my eventual goal with the form type is to automatically handle errors for most “standard” requests, and you can provide an error adaptor (imagine a laravel or django error adapter) that automatically structures error responses into the accepted setErrors format. We would leverage native fetch requests for you. An example API would be:

<FormKit
  type="form"
  endpoint="/api/users"
  method="post"
>
  <FormKit
    type="email"
    name="email"
  />
  <FormKit
    type="password"
    name="password"
   validation="required"
  />
  <FormKit
    type="password"
    name="password_confirm"
    validation="required|confirm"
  />
</FormKit>

Basically if the user provided an endpoint prop, we can assume responsibility for the submit handler and automatically catch any backend errors and set the on the form directly. This is an example of the kind of DX “enhancement” I’d like to see become reality, but what is currently published and available is still an early beta of the “fundamental” structures/api.

Read more comments on GitHub >

github_iconTop Results From Across the Web

6 Form Error Message Mistakes Almost Everyone Makes
In your error messages, you want to sound positive, calm, and straightforward. You should also avoid blaming anyone or anything. Inserting ...
Read more >
How to Report Errors in Forms: 10 Design Guidelines
1. Aim for Inline Validation Whenever Possible · 2. Indicate Successful Entry for Complex Fields · 3. Keep Error Messages Next to Fields...
Read more >
How to create simple JavaScript error message from form ...
As a good way, you may make an object, like {fieldName:{errorMessage,validationRule}} , where is fieldName - is your field name, that you need ......
Read more >
useForm - setError - React Hook Form
This method will force set isValid formState to false , however, it's important to aware isValid will always be derived by the validation...
Read more >
Form Validation Errors - how to track them and what to do ...
In this article, we'll try to cover all different types of errors and how to ... These errors usually happen after a user...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found