svelte-forms icon indicating copy to clipboard operation
svelte-forms copied to clipboard

[BUG] Form valid value not validating required field validator

Open luist1228 opened this issue 3 years ago • 4 comments

I have a simple sveltekit with tailwind page and I'm trying to do a simple signUp form with all the form fields required() . The thing is that the valid value of the form starts as true when all the values of the fields are empty and whenever a field is valid and the others are empty the value of form.valid is true.

<script lang="ts">
  import { form, field, style } from 'svelte-forms';
  import { required, email, matchField } from 'svelte-forms/validators';
  
  const userEmail = field('email', '', [required(), email()]);
  const password = field('password', '', [required()]);
  const confirmPassword = field('passwordConfirm', '', [required(), matchField(password)]);
  
  const signUpForm = form(userEmail, password, confirmPassword);
</script>

<label for="email" class="label">Email</label>
<input
  type="text"
  placeholder="Type your email"
  name="email"
  bind:value={$userEmail.value}
/>

<label for="password" class="password">Password</label>
<input
  type="password"
  placeholder="Type your password"
  name="password"
  bind:value={$password.value}
/>

<label for="confirmPassword" class="confirmPassword">Confirm password</label>
<input
  type="confirmPassword"
  placeholder="Confirm your password"
  name="confirmPassword"
  bind:value={$confirmPassword.value}
/>

I expected the inital value of the form.valid to be false on initialization and whenever for it to depend on the validity of the fields even when they are not dirty

image

image

In both cases the $form.valid should be false right?

luist1228 avatar Mar 24 '22 00:03 luist1228

Same here. Fyi once dirty the validation seems to kick in correctly, but initially I also got valid: true whatsoever.

casio avatar Apr 16 '22 21:04 casio

Same here. Fyi once dirty the validation seems to kick in correctly, but initially I also got valid: true whatsoever.

I don't know if you fully tested the library but in my case whenever a field is fully valid the entire form has valid as true, as I showed in the las image

luist1228 avatar Apr 17 '22 01:04 luist1228

Hi,

I'm looking for a fix! Thanks for the report

chainlist avatar Apr 17 '22 19:04 chainlist

FYIL @luist1228,

The form being valid at first render is an expected behavior. As stated in the documentation on the field section: image

When I change those options, it's seems to work as you would expect.

chainlist avatar Apr 17 '22 19:04 chainlist

Close issue due inactivity

chainlist avatar Jan 02 '23 21:01 chainlist