Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make input required dynamically?

I have a basic input component that looks like this:

<template>
  <div c>
    <label :class="required ? 'required' : ''">{{ label }}</label>
      <div >
      <input :value="modelValue" v-on:input="updateValue($event.target.value)"
             type="text"
      />
      </div>
      <p v-if="note" v-text="note"></p>
    </div>
</template>

<script setup>
defineProps({
  label: String,
  modelValue: String,
  required: { type: Boolean, default: false }
})

const emit = defineEmits(['update:modelValue'])

function updateValue(value){
  emit('update:modelValue', value);
}
</script>

I want that if the boolean required is passed that the "require" value is set on the input, i.e.

<input ... require>

How can I set that require option on the input? I don’t know how to set it, because its not of the typical key/value form. I also didn’t find anythign at https://vuejs.org/guide/essentials/forms.html#multiline-text

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just bind the required attribute to the required prop :

 <input :value="modelValue" v-on:input="updateValue($event.target.value)"
             type="text"
             :required="required"
      />

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading