search

Home  >  Q&A  >  body text

Learn how to use text fields for positive validation and send data

<p>I have a <code>v-text-field</code> with validation rules that trigger something when focus is lost. </p> <pre class="brush:php;toolbar:false;"><v-text-field :rules="validationRules" @blur="emitFunction" ></v-text-field></pre> <p>Is there a way to only call <code>emitFunction</code> when validation passes? </p>
P粉574695215P粉574695215479 days ago568

reply all(1)I'll reply

  • P粉025632437

    P粉0256324372023-08-10 15:10:39

    You can wrap it with v-form and use v-model to track the validity of the form

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

    reply
    0
  • Cancelreply