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.

Lazy validation trigger

See original GitHub issue

This function solves the problem (这个功能解决的问题)

As of now, i don’t see an option to change validation trigger behavior based on validation status. I am trying to achieve trigger on blur for non validated fields, if validation on blur throws an error it should change trigger to input. Once user fixes given field it should toggle back to on blur. It is in my opinion the best UX as user is not harassed as soon as he starts typing for example email. At the same time once he manages to enter incorrect value he sees he fixed the problem as soon as he corrects the value and does not need to focus out of the field.

Expected API (期望的 API)

As for the implementation maybe - and i have just flew over the code - it would be possible to get desired outcome just by accepting trigger as ref.

You could then do wrapper around validator like so:

export const makePasswordRule: FormItemRule = (options) => {
  let trigger = ref('blur');
  const passwordValidator: FormItemRuleValidator = (rule, value) => {
     const result = getValidationResult(rule, value); // at least 6 chars, required, ...
     trigger.value = result instanceof Error ? 'input' : 'blur';
     return result;
  }
  return {
    validator: passwordValidator,
    trigger: trigger
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
dav245commented, Jan 23, 2022

So basically here is a wrapper for any validator, that returns lazy validator.

const makeValidatorWithLazyTrigger = (
  validator: FormItemRuleValidator,
) => {
  const trigger = ref('blur');

  const wrappedValidator: FormItemRuleValidator = (...args) => {
    const result = validator(...args);

    trigger.value = result instanceof Error ? 'input' : 'blur';

    return result;
  };

  return reactive({
    trigger: trigger,
    validator: wrappedValidator,
  });
};

I was quite surprised no one was dealing with this issue before me. At least not publicly. Maybe this is something worth mentioning in the docs. Still i would firstly validate, that that weird thing with reactive around ref is valid solution and not something horrible to do.

Anyways thank you for your help. I will close this issue

0reactions
dav245commented, Jan 23, 2022

I am not totally sure why my solution is working. Maybe your insight will help. Here is working demo:

<script setup lang="ts">
const makePasswordRule: () => FormItemRule = () => {
  const trigger = ref('blur'); // Trigger is reactive
  const passwordValidator: FormItemRuleValidator = (rule, value) => {
    if (value.length < 6) {
      trigger.value = 'input';

      return new Error('password must be at least 6 characters long');
    }

    trigger.value = 'blur';
    return true;
  };

  return reactive( // and whole rule needs to be reactive too
    {
      validator: passwordValidator,
      trigger: trigger, // Linter says Ref<string> is not one of string | string[] | undefined
    }
  );
};

const rules = {
  password: makePasswordRule(),
};
</script>

<template>
  <n-form :rules="rules"></n-form>
</template>

I have tried some combinations: return nonreactive object / non ref trigger - validation works, trigger is NOT changed return nonreactive object / ref trigger - validation does not work at all return reactive object / ref trigger - everything works, just linter complains about ref<string> not being one of string | string[] | undefined return reactive object / non ref trigger - validation works, trigger is NOT changed

I honestly don’t know why combo reactive object ref trigger works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing Vuetify's Form Validation. Quite ... - Robert Mirabelle
If enabled, value will always be true unless there are visible validation errors. You can still call validate() to manually trigger validation.
Read more >
v-form API - Vuetify
Resets the state of all registered inputs (inside the form) to {} for arrays and null for all other values. Resets errors bag...
Read more >
[Feature Request] manual or reactive trigger of v-text-field ...
Problem to solve When using rules in v-text-field, "When the input value changes, each element in the array will be validated.
Read more >
How to trigger validation when user removes original field ...
I am well aware of JQuery validation's lazy behavior. In reality, it is only semi-lazy in that if a field contains a valid...
Read more >
Interaction Modes - VeeValidate
Interaction modes are used by vee-validate to determine when a validation should trigger. It is used as a replacement for the events prop....
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