Giter Club home page Giter Club logo

Comments (2)

wsmd avatar wsmd commented on May 31, 2024 3

Thanks for the feedback @romseguy! 😄

I'm planning on supporting custom validation errors in an upcoming release. Likely in 0.9.0. So I'm totally open to ideas.

I like your suggestion. It can be considered a breaking change though, since validity.name is expected to be truthy if the input valid. The object in your example will always be truthy whether the input is actually valid or not, so the following will break if it exists today:

{touched.name && !validity.name && (
  <div className="error">Name is invalid!</div>
)}

I've been thinking about taking a very similar approach where the validate() function returns either true indicating the form is valid, or any value other than true that will be treated as the error value and can be retrieved from state.errors. This should cover your use case above.

input.text({
  name: 'name',
  validate: value => DISALLOWED_CHARS_REGEX.test(value)
    ? { foo: 'hello', bar: 'world' } // anything returned is a validation "error"
    : true,
  ),
});

{
  values: {
    name: 'wsm*$#d'
  },
  validity: {
    name: false,
  },
  errors: {
    name: {
      foo: 'hello',
      bar: 'world'
    },
  }
}

// this continues to work (you could still reference errors)
{touched.name && !validity.name && (
  <div className="error">Name is invalid!</div>
)}

// or you could check for the error itself
{touched.name && errors.name && (
  <div className="error">{errors.name.foo}</div>
)}

Stay tuned!

from react-use-form-state.

KaRkY avatar KaRkY commented on May 31, 2024

I would recommend the folowing API for usage with validation messages.

<div class="form-control">
  <label {...input.label("name")}>Email address</label>
  <input {...input.text("name")} />
  {input.valid("name") && <span input.valid("name") />}
</div>

from react-use-form-state.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.