5

I'd bind a button to input using v-bind with a function :

The code inside my template :

<template>
  <input
    type="email"
    v-model="email"
    v-bind:placeholder="$t('fields.email_place_holder')"
  />
  <input
    v-bind:disabled="{ isDisable }"
    type="submit"
    v-bind:value="$t('buttons.notify_me')"
    class="notify-me-button"
  />
</template>

The code inside my script :

  methods: {
    isDisable() {
      return email.lenght > 0;
    }
  }

But the button does not change its status , I tried to change the css style with the same way and the result is the same.The problem is that button responds once time on the first value returned by isDisable().

4
  • 1
    Try converting isDisable to a computed property instead or replace isDisable with the condition you're checking for. i.e. <input :disabled="email.length > 0" type="submit" v-bind:value="$t('buttons.notify_me')" class="notify-me-button" /> Commented Nov 18, 2019 at 15:24
  • 1
    Directives requires only values not statements and conditions. Commented Nov 18, 2019 at 15:30
  • 1
    Also , when I test with console.log() I find that method executes once time not each time I type in the field. Commented Nov 18, 2019 at 15:38
  • 1
    If you choose to follow the directives using only values and not statements or expressions, then use a computed property computed: { emailLength: function () { return this.email.lenght > 0;}} The reason why the method executes only once is that it's not reactive. This is reactive and would change as the value of email changes. Commented Nov 18, 2019 at 15:54

3 Answers 3

10

The button isn't disabled because the binding disabled=isDisable is not reactive.

It's not reactive because the bound value isDisable is not a reactive property (in either data or computed) and it is not a method that has any reactive parameters i.e. it's called once/evaluated to the constant false. Vue won't check if there's reactive properties in the method body, after all methods are supposed to explicitly called in your code.

There's few ways you can fix this:

  1. Make isDisable a computed property instead:
computed: {
  isDisable() {
    return this.email.length > 0;
  }
}
  1. Pass a reactive property (e.g. email) as a parameter to your method so Vue knows to update the binding.
<input
  type="submit"
  :disabled="isDisable(email)"
/>
methods: {
  isDisable(email) {
    return email.length > 0;
  }
}
  1. Directly bind a statement or condition that includes your reactive property (email). This is allowed.
<input
  type="submit"
  :disabled="email.length > 0"
/>

Working example on JS Fiddle

These would be the obvious (and perhaps idiomatic) approaches to fix this, but there are others e.g. using a watcher.

Note there's also typos in your provided code, e.g.:

  • :disabled="{isDisable}" is an not a valid binding due to the curly braces
  • isDisable(){ return email.lenght > 0 } should be length not lenght
Sign up to request clarification or add additional context in comments.

Comments

5

The easy way to do it is to check if the value exists or not. For example:

<input type="text" v-model="user.name" required />

For the submit button just use disable

<button type="submit" :disabled="!user.name">Submit</button>

Only when the field is filled then the submit button is enabled for submit.

Comments

1

Try this:

<button type="button" class="btn btn-primary btn-sm ripple-surface" v- 
  bind:disabled='!isDisabled'>Save</button>
   computed: {
        isDisabled() {
        return this.categoryName.length > 0;
    }
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.