1

I would like to bind a class on the input field where this class is suppose to be applied. In my case I have input fields that I am dynamically creating and since I can't bind values with v-model for input fields that are dynamically created I can't bind the class with v-model data. This are the fields that I am creating inside of v-for loop:

  <template v-for="input in ninjaForm.fields">
    <div class="control">
      <input
        class="input is-large"
        :class="{ hasValue: input.value }"
        :ref="input.label.toLowerCase()"
        :type="input.type"
        :name="input.label.toLowerCase()"
        :required="input.required == 1">
      <label>{{ input.label }}</label>
    </div>
  </template>

I would like to know how can I bind a class now with that input field, so that I can check if the input field has some kind of value, so for example something like this:

:class="{ 'has-value' : this.input.value != ''}"

Hoe can I do that with Vue?

7
  • what is hasValue is it a css class or a data? Commented Dec 12, 2017 at 12:25
  • It is a css class Commented Dec 12, 2017 at 13:45
  • Do you want something like this? jsfiddle.net/943bx5px/12 if you enter color "red" or "green" you can see the input value bind to the class Commented Dec 12, 2017 at 13:51
  • But, like I stated in the question, I get an error when I bind class with v-model, since I am creating fields dynamically. Commented Dec 12, 2017 at 13:54
  • what fields are you creating dynamically? Please clarify your question. I don't even see your v-model in the example Commented Dec 12, 2017 at 13:55

1 Answer 1

1

I think that you're trying to do a class binding using the Object Syntax

In your example, to apply the CSS class hasValue if your input has any value that isn't falsy, you'll have something like this :

<input
  class="input is-large"
  :class="{ hasValue: input.value }"
  :ref="input.label.toLowerCase()"
  :type="input.type"
  :name="input.label.toLowerCase()"
  :required="input.required == 1">
<label>{{ input.label }}</label>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, my syntax was wrong but even with your correct syntax, I am not able to bind the class to any data value like the values from v-model, so that is what I am looking for, some way to bind a value with that specific input and not with some value from the component data object.

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.