Search code examples
vue.jsvuetify.js

How to only emit from text-field after positive validation


I have a v-text-field with validation rules, which emits something on blur.

<v-text-field
    :rules="validationRules"
    @blur="emitFunction"
></v-text-field>

Is there a way to only call the emitFunction if the validation passes?


Solution

  • You can wrap it in v-form, and track if the form is valid with v-model

    const isValid = ref(false)
    function emitIfValid() {
      if (isValid.value) emitFunction()
    }
    
    <v-form v-model="isValid">
      <v-text-field
        :rules="validationRules"
        @blur="emitIfValid"
      />
    </v-form>