Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report][3.4.7] Changing :rules of Vuetify Inputs does not affect the validation #18875

Closed
peter-mueller opened this issue Dec 14, 2023 · 8 comments
Assignees
Labels
E: validation wontfix The issue is expected and will not be fixed
Milestone

Comments

@peter-mueller
Copy link

peter-mueller commented Dec 14, 2023

Environment

Vuetify Version: 3.4.7
Last working version: 2.3.0
Vue Version: 3.4.0-beta.2
Browsers: Chrome 119.0.0.0
OS: Windows 10

Steps to reproduce

Warning: possible duplicate, could be closed because of high complexity, but i've found no workaround to make it work securely in my big application, read other comments

  • Input "aa"
  • Enable the checkbox (replaces the rules array with different rules)
  • Focus Input
  • Blur Input

Expected Behavior

Changing the Rules should immediately change the valid status of the form to not allow invalid inputs/confusion of the user. The error message should be immediately displayed.

Actual Behavior

Dynamically replacing the rules array of an input does not trigger a revalidation. This leaves the form valid, until the user revisits the input.

Reproduction Link

https://play.vuetifyjs.com/#...

Other comments

The behavior of showing/hiding the error message on first form input of the user is already very complex. Dynamic rules make this even more complex. Should a changing rule always make the Input immediality show the error or only if its already dirty/has some input? I could'nt test some combinations, because i failed to find/access the isDirty/isPrestine state of the input.

I've found #8698 (comment). And tried the validate(silent: true) option, which didn't help me because i couldn't find a isDirty state. The <v-validation :rules="[rules]"> is not usable to me because i've found no documentation on how to use it with all the existing variants of inputs, having to write the inputs on my own would make me question the use of the framework for my size of application.


Edit: The following snippet is my current wip workaround in my Compontent that wraps v-text-field that does not completly work because the way of getting the silent value is not robust/consistent on prisitine/dirty inputs.

const vtextfield = ref<
  | (ComponentPublicInstance & { validate: (silent: boolean) => Promise<string[]>,})
  | null
>(null);

watch(() => props.rules, () => {
  nextTick(() => validate());
}, {deep: true})

async function validate(): Promise<void> {
  const silent = props.modelValue === '' || props.modelValue === null || props.modelValue === undefined;
  await vtextfield.value?.validate(silent);
}

Edit2: Another Workaround i've found looks like this (⚠️ didn't work consistent for me):

<v-text-field ref="vtextfield">
    <template v-slot:append="{isDirty}">
       {{_workaroundDirty(isDirty.value)}}
    </template>
</v-text-field>

<script lang="ts">

watch(() => props.rules, () => {
  nextTick(() => validate());
}, {deep: true})

const dirty= ref<boolean>(false);

function _workaroundDirty(isDirty: boolean): string {
  dirty.value = isDirty;
  return "";
}

async function validate(): Promise<void> {
  const silent = !dirty.value;
  await vtextfield.value?.validate(silent);
}
</script>

Edit 3: Because we had to move on in our project we decided to replace the rules evaluation and v-form with our own typescript code and just pass errors to the inputs. Therefore the issue is no longer relevant to us. I'll leave it open to document that the migration was quite effortful and others might arrive at this problem.

@peter-mueller peter-mueller changed the title [Bug Report][3.4.7] Changing :rules of Vuetify Inputs does not affect the displayed message [Bug Report][3.4.7] Changing :rules of Vuetify Inputs does not affect the validation Dec 18, 2023
@qxygene
Copy link

qxygene commented Dec 20, 2023

_rules must be reactive.

const _rules = reactive({
  required: (v) => !!v || 'required',
  isValid: (v) => /^.{3,}$/.test(v) || 'min 3',
});

@peter-mueller
Copy link
Author

  function _rules(additionalrule: boolean) {
   if (additionalrule) {
      return [isRequired, atleastlength3];
   }
   return [isRequired];

I made sure to return new arrays and not modify them to avoid reactivity issues in the example of the issue.


Because we had to move on in our project we decided to replace the rules evaluation and v-form with our own typescript code and just pass errors to the inputs. Therefore the issue is no longer relevant to us. I'll leave it open to document that the migration was quite effortful and others might arrive at this problem.

@Martin-Idel
Copy link
Contributor

@qxygene: Reactivity seems not to help. We have the same problem in various components and the rules are computed. This is a regression with respect to Vuetify 2.

@billybraga
Copy link

This is affecting us also.

@ewitz

This comment was marked as off-topic.

@santhikumar43
Copy link

Rules are not reactive, This is creating lot of problems.
Dependent fields are not working Vuetify Playground

Here the last name is dependent on first name, once first name is filled then last name field should show as a required automatically and it was working in vuetify v2 but now i need to click on submit or focus in and focus out of the last name field to show that field as required.

@laventnc
Copy link

laventnc commented Oct 2, 2024

Was about to create a similar issue report and stumbled upon this.

For another example: see this playground

The required rule on the 'First Name' field is only set if the 'Last Name' field has a value. Therefore, clearing the 'Last Name' should remove the error state/message. The error state is removed, but the message still remains

@johnleider johnleider self-assigned this Feb 10, 2025
@johnleider johnleider added T: bug Functionality that does not work as intended/expected E: validation and removed S: triage labels Feb 10, 2025
@johnleider johnleider added this to the v3.7.x milestone Feb 10, 2025
@johnleider johnleider added wontfix The issue is expected and will not be fixed and removed T: bug Functionality that does not work as intended/expected labels Feb 11, 2025
@johnleider
Copy link
Member

@johnleider johnleider closed this as not planned Won't fix, can't repro, duplicate, stale Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E: validation wontfix The issue is expected and will not be fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants