GithubHelp home page GithubHelp logo

dvingo / malli-react-hook-form Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 0.0 250 KB

Sample app demonstrating how to integrate malli with react-hook-form

Home Page: https://dvingo.github.io/malli-react-hook-form/

License: MIT License

HTML 2.61% Clojure 97.39%
clojurescript malli react-hook-form

malli-react-hook-form's Introduction

This repo demonstrates how to use malli schemas to validate forms with react-hook-form

You can see the running example on github pages:

dvingo.github.io/malli-react-hook-form

To integrate in your own codebase the main point of integration is a malli validation helper to output errors in the shape used by react-hook-form:

(defn use-malli-resolver [schema]
  (let [validator (m/validator schema)
        explain   (m/explainer schema)]
    (hooks/use-callback [schema]
      (fn [data-js]
        (let [data   (js->clj data-js :keywordize-keys true)
              valid? (validator data)]
          (if valid?
            #js{:values data :errors #js{}}
            (let [errs (reduce-kv
                         (fn [errs field messages]
                           (doto errs
                             (g/set (name field) #js{:type "validation" :message (first messages)})))
                         #js{}
                         (me/humanize (explain data)))]
              #js{:values #js{} :errors errs})))))))

;; then in your render function use this "resolver":

(let [resolver      (use-malli-resolver form-schema)
      form-methods  (useForm #js{:resolver resolver})

Running locally

prerequisites:

node+npm java clojure yarn 1.x

After cloning the repo:

yarn install
yarn shadow-cljs watch main

navigate to http://localhost:4023

Credits

The idea of making reusable form input components on top of controlled UI libraries comes from:

https://koprowski.it/react-native-form-validation-with-react-hook-form-usecontroller/

and figuring out how to use custom validation is from the react-hook-form docs

https://react-hook-form.com/advanced-usage#CustomHookwithResolver

malli-react-hook-form's People

Contributors

dvingo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.