Correct way to wrap an input?
See original GitHub issueVersions:
- VueJs: 2.5.2
- Vee-Validate: 2.0.0-rc.26
Description:
I’ve tried many ways to wrap an <input>
with support for v-model
and v-validate
. The only way I found is:
<template>
<div>
<label>{{label}}</label>
<input type="text" v-model="innerValue" :name="name" v-validate="constraints">
<template v-if="constraints !== ''">
<span class="validation-status validation-status--invalid" v-if="errors.has(name)">❌</span>
<span class="validation-status validation-status--valid" v-else>✅</span>
</template>
</div>
</template>
<script>
export default {
props: {
name: {
required: true
},
value: {
required: true
},
label: String,
constraints: {
type: String,
default: ''
}
},
data () {
return {
innerValue: null
}
},
created () {
this.innerValue = this.value
},
watch: {
innerValue (newVal) {
this.$emit('input', newVal)
},
value (newVal) {
this.innerValue = newVal
}
}
}
</script>
Any other way would cause errors from vee-validate.
This works fine but it looks like a lot of code. Is there a better way to create such a component ?
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Wrapping text inside input type="text" element HTML/CSS
I have investigated using this technique, and it has some caveats. I love that it automatically sizes correctly, but it brings in a...
Read more >Wrapping and breaking text - CSS: Cascading Style Sheets
Wrapping and breaking text. This guide explains the various ways in which overflowing text can be managed in CSS.
Read more >HTML textarea wrap Attribute - W3Schools
The wrap attribute specifies how the text in a text area is to be wrapped when submitted in a form. Browser Support. Attribute....
Read more >How to Wrap text in Html - Javatpoint
Step 2: Now, we have to use word-wrap property. So, we have to place the cursor between the head tag just after the...
Read more >How to wrap text within an input field in a table? - Jotform
Hello, I am trying to find the best way to do this. There is a lot of information typed into each box within...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There is no “correct” way to do it, but I generally prefer to create input components that “work” with
vee-validate
, for example this is a component we use in some of our projects:As you can see, there is no need for the
innerValue
here, but that means the component doesn’t know its state, and any errors or state must be passed to it like theerror
property:What you are doing is creating a “self-validating” component, which while is nice, could produce some state issues depending on the implementation like you’ve noticed.
I would like to add more improvements to the custom components validation, like the ability to inject the field state into it if its being validated by
vee-validate
which should make it easier to author components.This is my version. I disable the automatic validation (v-validate.disable) and makes a manual validation on blur and on input when the content has been validated and is invalid. I make it possible for the consumer to attach listeners or attributes at will, at the same time.