1

I'm using reusable code for my contact form, and there are two required fields. In the parent component, i use label as the name of the label tag. For an example, the label full name must have 'Full Name ' and the '' should be in blue color. I don't have any idea on how to do that. Appreciate if someone could help me.

InputField.vue

<template>
  <label for="name">
        {{ label }}
        <span style="color:#0077FF">&nbsp;*</span>
      </label>
</template>

Contact.vue

<form id="contact-form" @submit.prevent="submitForm">
  <InputField
    v-model="fullName"
    label="Full Name *"
    placeholder="Your full name"
    required
    @handleChange="handleChangeName($event)"
  />
</form>
2
  • Please provide any live snippent/demo ? Commented Sep 26, 2019 at 5:54
  • @NikleshRaut it just a simple label that you see on any contact form from any websites. This is the design, but i dont know how to change the star symbol to be blue(that means it is required) imgur.com/a/8Fp0aMX Commented Sep 26, 2019 at 6:06

1 Answer 1

2

Probably easiest to do this in CSS.

Add a required prop to your component (if you didn't have one already) and use it to add a CSS class to the label

props: {
  label: String,
  required: Boolean,
  placeholder: {
    type: String,
    required: false
  }
}
<label :class="{ required }">{{ label }}</label>
<style scoped>
label.required:after {
  content: ' *';
  color: #0077FF;
}
</style>

Then you can just supply the label without decoration and the CSS will add it automatically for required fields.

<InputField required label="Full Name" .../>
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.