GithubHelp home page GithubHelp logo

kenoxa / svelte-formup Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 924 KB

forms with yup for svelte

Home Page: https://svelte-formup.js.org

License: MIT License

JavaScript 0.27% HTML 4.66% TypeScript 95.08%
form svelte validation yup

svelte-formup's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar kimskovhusandersen avatar sastan avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

svelte-formup's Issues

invalid, errors and dirty readable stores from formup do not show correct values

Hi Sascha,

I'm displaying a form with a single field.

When selecting the field, the $dirty store does not update. It remains an empty object.
When I enter a text and delete it again, the $errors and $invalid store do not update either. Both remain empty objects.

{ "values": { "bpm_comment": "" }, "invalid": {}, "errors": {}, "dirty": {} }

I'm using

  • svelte-formup version 0.6.2,
  • svelte version 3.32.0, and
  • yup version 0.30.0

Here is my code:

<script lang="ts">
  import { object, string } from 'yup'
  import { formup } from 'svelte-formup'

  const schema = object().shape({
    bpm_comment: string().required(),
  })

  const { values, validate, invalid, errors, dirty, validity } = formup({
    getInitialValues: () => ({
      bpm_comment: undefined,
    }),
    validateOn: ['input', 'change'],
    schema,
    validateInitialValues: true,
    async onSubmit(data, _) {
      console.log({ data })
    },
  })

</script>

<form use:validate role="form">
  <div use:validity>
    <textarea
      id={'bpm_comment'}
      name="bpm_comment"
      label="Comment (optional)"
      rows={3}
      bind:value={$values.bpm_comment} />
  </div>
  <p>
    <button type="submit">Submit</button>  
    <button type="reset">reset</button></p>
</form>

{JSON.stringify({ values: $values, invalid: $invalid, errors: $errors, dirty: $dirty }, null, 4)}

Initial validation does not work as expected

Hi Sascha,

When rendering the form initially with validateInitialValues: true, the $invalid stores is empty and $invalid.get("bwcl_employee") returns undefined.

When selecting a different value and then selecting the initial value ("undefined") again, the errors shows correctly:
ValidationError: bwcl_employee is a required field

I'm using

  • svelte-formup version 0.6.2,
  • svelte version 3.32.0, and
  • yup version 0.30.0

Here is my code:

<script lang="ts">
  import { object, string } from 'yup'
  import { formup } from 'svelte-formup'

  const schema = object().shape({
    bwcl_employee: object({
      id: string().required(),
      manager: object()
        .shape({
          id: string().required(),
        })
        .required(),
    })
      .required()
      .defined(),
  })

  schema.validate({}, { abortEarly: false }).catch((error) => {
    console.log({ error })
  })

  const { values, validate, invalid, errors, dirty, validity, isDirty, isError } = formup({
    getInitialValues: () => ({
      bwcl_employee: undefined,
    }),
    validateOn: ['input', 'change'],
    schema,
    validateInitialValues: true,
    onSubmit(data, _) {
      console.log({ data })
    },
  })
</script>

<form use:validate role="form">
  <div use:validity>
    <select id="bwcl_employee" name="bwcl_employee" bind:value={$values.bwcl_employee}>
      <option>please select</option>
      <option value={{ name: 'hans' }}>xy</option>
      <option value={{ id: 'xy', name: 'hans' }}>xy</option>
      <option value={{ id: 'zw', name: 'hans' }}>zw</option>
    </select>
    <p>{$invalid?.get('bwcl_employee')}</p>
  </div>
  <p><button type="submit">Submit</button> <button type="reset">reset</button></p>
</form>

{JSON.stringify({ values: $values, invalid: $invalid, errors: $errors, dirty: $dirty, isDirty, isError }, null, 4)}


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.