1

I am making a form in a Vue component and would like to set the HTML attribute required to an input field based on the value I am having in an object property. So, for the example, an object that has fields like this:

label:"Name"
required:"1"
type:"textbox"

I need to a set the field to have required attribute in an input tag:

<input class="input is-large" :type="input.type" required>

And for the ones that don't have 1 as a value for that field I don't want a required attribute. How can I do that in Vue?

1 Answer 1

2

You can do it like this:

<input class="input is-large" :type="input.type" :required="obj.required == 1">

Since your object's required property has 1 as a string not number I used == for comparison so that equality is tested after coercion

Sign up to request clarification or add additional context in comments.

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.