Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if rule is failing in vuetify's v-text-field?

How do I know if a "rule" is failing in vuetify? I have a text field

<v-text-field label="Friendly URL" :rules="[rules.friendlyUrl]" v-model="friendlyUrl"></v-text-field>

And my rules definition:

data () {
  return {
    rules:{
      friendlyUrl(value){
        console.log('validating value');
        if (/^[a-z0-9\-]*$/.test(value)){
          return true;
        }
        return 'only lowercase letters, numbers and dashes are allowed'
      }
    }
  }

I want to "watch" something to know if my rule has failed or not. That way I can disable a submit button until all rules have passed.

Bonus points if you know how to set this up for multiple text fields.

like image 920
RoccoB Avatar asked Sep 18 '17 19:09

RoccoB


People also ask

What is lazy validation Vuetify?

lazy-validation. boolean. false. If enabled, value will always be true unless there are visible validation errors. You can still call validate() to manually trigger validation.

How do you delete a text field in Vuetify?

You can use the @click:clear="()" so you can clear your text at the same time it will call the function. Show activity on this post. Use the clear-icon-cb prop. This allows you to use a custom callback function when the clear icon when clicked.

How do I add validation to Vuetify?

Go to the script tag outside the template tag and write the code below: <v-text-field v-model="firstname" :rules="nameRules" label="First Name" required> </v-text-field> firstname: '', lastname: '', nameRules: [ v => !! v || 'Name is required', v => (v && v.

Does Vuetify work with vue3?

The current version of Vuetify does not support Vue 3.

How do I validate an shaped text field in vuetify?

shaped text fields are rounded if they’re outlined and have higher border-radius if filled. Vuetify includes simple validation through the rules prop. The prop accepts a mixed array of types function, boolean and string. When the input value changes, each element in the array will be validated.

How do I validate if a text field is rounded?

shaped text fields are rounded if they’re outlined and have higher border-radius if filled. Vuetify includes simple validation through the rules prop. The prop accepts a mixed array of types function, boolean and string.

When does the validation event occur for a V-text field?

Based on this definition, the validation event occurs only for the specific v-text-field that changes, without an option to trigger it manually; not even by clicking on the input. As a basic use-case, this may be necessary whenever validation conditions depend on any state of other inputs.

How do I prepend non-modifiable text to a text field?

The prefix and suffix properties allows you to prepend and append inline non-modifiable text next to the text field. shaped text fields are rounded if they’re outlined and have higher border-radius if filled. Vuetify includes simple validation through the rules prop. The prop accepts a mixed array of types function, boolean and string.


1 Answers

Vuetify will do this for you if you use their v-form component.

<v-form v-model="valid">
  <v-text-field label="Friendly URL" :rules="[rules.friendlyUrl]" v-model="friendlyUrl"></v-text-field>
</v-form>

Where, valid is a boolean data property. You can use it to toggle buttons, set classes, etc.

like image 123
Bert Avatar answered Oct 02 '22 11:10

Bert