Can I access a data element in a rule?
Here is my code running
I'm trying to flip the value of a data element on a text field rule in a Vuetify form.
The rule itself works fine, however I'm unable to access the data element, I'm getting this error:
TypeError: Cannot set property 'disabled' of undefined
Here is my code:
data: function() {
return {
  disabled: false,
  rules:{
    sellerId(value){
      if(value.length == 0){
        this.disabled = true;
        return "What are you trying to do here?";  
      }
      else{
        return true;
      }
    }
  },
What am I doing wrong?
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.
A ref allows us to access internal methods on a component, for example, <v-form ref="form"> . this. $refs.
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.
rules are an array of functions, and if you need the function to be able to access data property, you can define them as component methods:
data: function () {
  return {
    disabled: false
  }
},
methods: { 
  sellerId (value) {
    if (value.length === 0) {
      this.disabled = true;
      return "What are you trying to do here?";  
    } else {
      return true;
    }
  }
}
And then in your Vuetify component:
<v-text-field :rules="[ sellerId ]"></v-text-field>
                        try to define rules as computed property :
data: function() {
    return {
      disabled: false,
      ...
    }
  },
  computed: {
    sellerIdRules() {
      return [
         (v) => {
        if (value.length == 0) {
          this.disabled = true;
          return "What are you trying to do here?";
        } else {
          return true;
        } ]
      }
    }
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With