In my application I have a lot of forms and most inputs look exactly like this:
<div class="form-group">
<label for="language">{{ $t('form.language')}}</label>
<input type="text" class="form-control" id="language" name="form.language" v-model="language" v-validate.initial="'required'" :data-vv-as="$t('form.language')" />
<span class="invalid-feedback">{{ errors.first('language') }}</span>
</div>
This gets duplicated over and over again. The only thing that really changes is the name of the field and the input type. Sometimes it is a select and sometimes it is a more complex component instead of simple HTML one.
My idea is to create some kind of wrapper component. So I dont have to copy all this and simply use something like that:
<form-group name="language">
<input type="text" v-model="form.language">
</form-group>
I tried to implement it that way, but it doesnt work:
<template>
<div class="form-group">
<label :for="name">{{ $t('form.' + name)}}</label>
<slot class="form-control"
:id="name"
:data-vv-name="name"
v-validate.initial="'required'"
:data-vv-as="$t('form.'+ name)">
</slot>
<span class="invalid-feedback">{{ errors.first(name) }}</span>
</div>
</template>
<script>
export default {
props: ['name']
}
</script>
Do you have any ideas? The problem is that I cannot pass mixins and props easily to the slotted element/component.
The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this attribute is set, the element matches the :required UI pseudo-class and the form won't submit, displaying an error message on submission when the input is empty.
If you have data restrictions in place and a user enters invalid data into a cell, you can display a message that explains the error. Select the cells where you want to display your error message. On the Data tab, click Data Validation > Data Validation.
All you need is to add the v-validate directive to the input you wish to validate and make sure your input has a name attribute for error messages generation. Then, pass to the directive a rules string which contains a list of validation rules separated by a pipe ' | '.
HTML novalidate attribute The novalidate attribute in HTML is used to signify that the form won't get validated on submit.
How about scoped slots (Vue 2.5.0+)?
<!-- form-group.vue -->
<template>
<div>
<label />
<slot v-bind="$props" />
<span />
</div>
</template>
Above, all the props of <form-group>
is bound to the slot using v-bind
. You may want to specify certain fields only: <slot :id="name" :data-vv-name="name" />
<form-group name="age">
<input type="number" slot-scope="slotProps" v-bind="slotProps" />
</form-group>
Here, the <input>
can access the slot props by using slot-scope
, giving it a name (here slotProps
). slotProps
will contain all the props of <slot>
as defined in form-group.vue
.
Some more examples:
<form-group name="language">
<input type="text" slot-scope="sp" v-bind="sp" />
</form-group>
<form-group name="hello" value="friend">
<span slot-scope="sp">
{{ sp.name }}: {{ sp.value }}
</span>
</form-group>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With