dynamic validation isnt work in vuelidate - vue.js

I want returns password and password confirm validation only if the user wants to change the password.
validations() {
if(this.changePassword) {
return {
editedItem : {
account_id : {
required,
minLength : minLength(4),
maxLength : maxLength(15)
},
name: {
required,
minLength: minLength(4),
maxLength: maxLength(20)
},
password: {
required,
minLength: minLength(4),
maxLength: maxLength(20)
},
passwordConfirm: {
required,
sameAsPassword: sameAs('password')
}
}
}
}
else {
return {
editedItem : {
account_id : {
required,
minLength : minLength(4),
maxLength : maxLength(15)
},
name: {
required,
minLength: minLength(4),
maxLength: maxLength(20)
},
}
}
}
},
computed: {
accountIdErrors () {
const errors = []
if (!this.$v.editedItem.account_id.$dirty) return errors
!this.$v.editedItem.account_id.minLength && errors.push('ID should be 4 to 15 characters')
!this.$v.editedItem.account_id.maxLength && errors.push('ID should be 4 to 15 characters')
!this.$v.editedItem.account_id.required && errors.push('ID required')
return errors
},
nameErrors () {
const errors = []
if (!this.$v.editedItem.name.$dirty) return errors
!this.$v.editedItem.name.minLength && errors.push('Name should be 4 to 20 characters')
!this.$v.editedItem.name.maxLength && errors.push('Name should be 4 to 20 characters')
!this.$v.editedItem.name.required && errors.push('Name required')
return errors
},
pwErrors () {
const errors = []
if (!this.$v.editedItem.password.$dirty) return errors
!this.$v.editedItem.password.minLength && errors.push('Password should be 4 to 20 characters')
!this.$v.editedItem.password.maxLength && errors.push('Password should be 4 to 20 characters')
!this.$v.editedItem.password.required && errors.push('Password required')
return errors
},
pwConfirmErrors() {
const errors = []
if (!this.$v.editedItem.passwordConfirm.$dirty) return errors
!this.$v.editedItem.passwordConfirm.sameAsPassword && errors.push('Not same as password')
return errors
},
}
Below is the form. The "changePassword" variable depends on the v-switch.
<v-dialog
v-model="editDialog"
max-width="500px"
>
<v-card>
<v-card-title>
<span class="text-h5"> Edit Account </span>
</v-card-title>
<v-card-text>
<v-container>
<v-form>
<v-row>
<v-text-field
:disabled="!createMode"
v-model="editedItem.account_id"
:error-messages="accountIdErrors"
label="Account ID"
hide-details="auto"
#input="$v.editedItem.account_id.$touch()"
#blur="$v.editedItem.account_id.$touch()"
></v-text-field>
</v-row>
<v-row>
<v-text-field
v-model="editedItem.name"
:error-messages="nameErrors"
label="Name"
hide-details="auto"
#input="$v.editedItem.name.$touch()"
#blur="$v.editedItem.name.$touch()"
></v-text-field>
</v-row>
<v-row>
<v-col>
//if the "changePassword" becomes true, password validation should be enabled
<v-switch
:disabled="createMode"
v-model="changePassword"
label="Change Password"
#click="editPwclear"
></v-switch>
<v-text-field
v-if="changePassword"
v-model="editedItem.password"
:error-messages="pwErrors"
label="New Password"
hide-details="auto"
type = "password"
#input="$v.editedItem.password.$touch()"
#blur="$v.editedItem.password.$touch()"
></v-text-field>
<v-text-field
v-if="changePassword"
v-model="editedItem.passwordConfirm"
:error-messages="pwConfirmErrors"
label="Confirm Password"
hide-details="auto"
type = "password"
#input="$v.editedItem.passwordConfirm.$touch()"
#blur="$v.editedItem.passwordConfirm.$touch()"
></v-text-field>
</v-col>
</v-row>
</v-form>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
color="blue darken-1"
text
#click="close"
>
Cancle
</v-btn>
<v-btn
color="blue darken-1"
text
#click="editSubmit"
>
Save
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
editPwclear() that runs when the v-switch is clicked. Initializes the value to an empty value.
editPwclear(){
this.editedItem.password = ''
this.editedItem.passwordConfirm = ''
this.$v.$reset()
},
Even if I enter the correct value, a length error always occurs.

Related

vue.js not responding to property of object changing

<v-container class="text-center hyp-container pa-4">
<v-row>
<button #click="toggleForm">Add new</button>
</v-row>
<v-row>
<v-dialog v-model="showConfirmDelete" width="500">
<v-card>
<v-card-title class="headline grey lighten-2">
Confirm
</v-card-title>
<v-card-text>
<v-spacer></v-spacer>
Are you sure you want to delete this target?
</v-card-text>
<v-card-actions>
<v-btn color="secondary" #click="showConfirmDelete = false"
>Cancel</v-btn
>
<v-spacer></v-spacer>
<v-btn color="primary" #click="doDeleteTarget">Delete</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
<v-dialog v-model="showTargetForm" width="800">
<v-card>
<form #submit="saveTarget">
<v-card-title class="headline grey lighten-2">
Add Slack target
</v-card-title>
<v-card-text>
<v-spacer></v-spacer>
<v-text-field
label="Target Name"
v-model="target.name"
#keyup="modifyTargetName"
></v-text-field>
<v-text-field
label="Webhook URL"
v-model="target.webhook"
></v-text-field>
<v-text-field
label="Slack Channel"
v-model="target.channel"
#keyup="modifyTargetChannel"
></v-text-field>
</v-card-text>
<v-divider></v-divider>
<v-card-actions>
<v-btn color="secondary" #click="showTargetForm = false"
>Close</v-btn
>
<v-spacer></v-spacer>
<v-btn color="secondary" #click="target = {}">Clear</v-btn>
<v-btn color="secondary" #click="testTarget">Test</v-btn>
<v-btn color="primary" type="submit">
Save
</v-btn>
</v-card-actions>
</form>
</v-card>
</v-dialog>
</v-row>
<v-row>
<ol>
<li v-for="target in targets" :key="target.id">
<v-card>
<v-card-text>
<v-card-actions>
<v-btn icon #click="toggleShow(target)">
<v-icon>{{
target.show ? "mdi-chevron-up" : "mdi-chevron-down"
}}</v-icon>
</v-btn>
<div class="title">
{{ target.name }}
{{ target.show }}
</div>
</v-card-actions>
<v-expand-transition>
<div v-if="target.show">
<v-divider></v-divider>
<v-card-text>
{{ target.channel }}
edit
delete
<div class="truncate">{{ target.webhook }}</div>
</v-card-text>
</div>
</v-expand-transition>
</v-card-text>
</v-card>
</li>
</ol>
</v-row>
</v-container>
</template>
<script>
export default {
name: "PersonalTargetsSelector",
props: {
disabled: {
type: Boolean,
default: false,
},
},
data: () => ({
showTargetForm: false,
target: {},
targets: [],
showConfirmDelete: false,
targetToDelete: {},
show: {},
}),
async mounted() {},
methods: {
toggleShow(target) {
console.log(target.show);
this.target.show = !Boolean(target.show);
console.log(target.show);
},
handleClickCancel: function () {
this.target = {};
this.showTargetForm = false;
this.handlePageTrackerEvent(
EVENT_CATEGORIES.personalTargets,
EVENT_ACTIONS.clicked,
"Edit Mode - Cancel a Personal Target"
);
},
saveTarget(e) {
e.preventDefault();
console.log("Save target....", this.target);
if (this.target.id) {
// update
this.targets.map(
(t) =>
this.targets.find((t) => t.id === this.target.id) || this.target
);
} else {
// create
this.target.id = Math.random().toString(26).slice(2);
this.targets.push(this.target);
}
this.target = {};
this.showTargetForm = false;
},
editTarget(target) {
this.target = target;
this.showTargetForm = true;
},
testTarget() {
console.log("test target...");
},
confirmDeleteTarget(target) {
this.showConfirmDelete = true;
this.targetToDelete = target;
},
doDeleteTarget() {
this.targets = this.targets.filter(
(t) => t.id !== this.targetToDelete.id
);
this.showConfirmDelete = false;
},
modifyTargetChannel(e) {
const { value } = e.target;
console.log(value);
this.target.channel = value.indexOf("#") === -1 ? "#" + value : value;
},
modifyTargetName(e) {
const { value } = e.target;
this.target.name =
value.indexOf("[Personal]") === -1 ? "[Personal] " + value : value;
},
toggleForm(e) {
e.preventDefault();
this.showTargetForm = !this.showTargetForm;
},
handleClickClose() {
this.forceClose = new Date().toISOString();
},
setConfirmationShow(value) {
this.resetConfirmationShow = value;
},
handlePageTrackerEvent(category, action, name) {
let _paq = (window._paq = window._paq || []);
_paq.push(["trackEvent", category, action, name]);
pageTrackerLogger(
"Page Tracker Event " +
category +
" " +
action +
" " +
name +
" logged."
);
},
},
watch: {
"target.show"(newValue) {
console.log(newValue);
},
},
};
</script>
<style>
.v-application ol {
list-style-type: none;
padding: 0;
}
</style>
the log shows its changing but the UI does not update its value when i put it in {{target.show}}
You're updating target through the props passed to function. Instead:
methods: {
toggleShow(target) {
console.log(target.show);
this.target.show = !Boolean(this.target.show);
console.log(target.show);
},
}

Vuetify datatable crud on click "new"

I using example from documentation datatable with crud options.
How to change "New" on-click function?
What is v-slot:activator="{ on, attrs }"?
I need to call api with vuex dispatch.
I want that before edit dialog is opened row will immediately created with ID parameter inside from api.
I added #click, to make call. Is it correct?
<template>
<v-data-table
:headers="headers"
:items="PIATConsignments"
:footer-props="footerProps"
class="elevation-1"
>
<template v-slot:top>
<v-toolbar
flat
>
<v-toolbar-title>Items</v-toolbar-title>
<v-divider
class="mx-4"
inset
vertical
></v-divider>
<v-spacer></v-spacer>
<v-dialog
v-model="dialogEdit"
scrollable
max-width="800px"
>
<template v-slot:activator="{ on, attrs }">
<v-btn
color="primary"
dark
class="mb-2"
v-bind="attrs"
v-on="on"
#click="newItem"
>
New
</v-btn>
</template>
<v-card>
<v-card-title>
<span class="text-h5">{{ formTitle }}</span>
</v-card-title>
<v-card-text style="height: 600px;">
<v-container>
<v-row>
<v-col
cols="12"
sm="12"
md="6"
>
<v-autocomplete
v-model="editedItem.DepartureCountryDetails"
:items="countries"
item-text="ShortCountryName"
label="Departure"
class="input-group--focused"
v-bind:readonly="isReadonly"
return-object
></v-autocomplete>
</v-col>
<v-col
cols="12"
sm="12"
md="6"
>
<v-autocomplete
v-model="editedItem.DestinationCountryDetails"
:items="countries"
item-text="ShortCountryName"
label="Destination"
class="input-group--focused"
v-bind:readonly="isReadonly"
return-object
></v-autocomplete>
</v-col>
</v-row>
<v-row>
<v-col
cols="12"
sm="12"
md="6"
>
<v-text-field
v-model="editedItem.CAInvoiceValueAmount"
label="Amount"
></v-text-field>
</v-col>
<v-col
cols="12"
sm="12"
md="6"
>
<v-text-field
v-model="editedItem.UnifiedGrossMassMeasure"
label="Weight"
></v-text-field>
</v-col>
</v-row>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
color="blue darken-1"
text
#click="close"
>
Close
</v-btn>
<v-btn
color="blue darken-1"
text
#click="save"
>
Save
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
<v-dialog v-model="dialogDelete" max-width="500px">
<v-card>
<v-card-title class="text-h5">Do you want to delete item?</v-card-title>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text #click="closeDelete">Нет</v-btn>
<v-btn color="blue darken-1" text #click="deleteItemConfirm">Да</v-btn>
<v-spacer></v-spacer>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.actions="{ item }">
<v-icon
small
class="mr-2"
#click="editItem(item)"
>
mdi-pencil
</v-icon>
<v-icon
small
#click="deleteItem(item)"
>
mdi-delete
</v-icon>
</template>
</v-data-table>
</template>
<script>
import modelPIATConsignment from '#/models/form/consignment/piat/PIATConsignment'
import DepartureCountryDetails from './DepartureCountryDetails.vue'
export default {
name: 'PIATConsignmentDetailsTable',
components: {
DepartureCountryDetails,
},
props: {
consignmentForm: { Object },
},
data: function() {
return {
dialogEdit: false,
dialogDelete: false,
headers: [
{ text: 'Departure', value: 'DepartureCountryDetails.ShortCountryName' },
{ text: 'Destination', value: 'DestinationCountryDetails.ShortCountryName' },
{ text: 'Amount', value: 'CAInvoiceValueAmount' },
{ text: 'Weight', value: 'UnifiedGrossMassMeasure' },
{ text: 'Actions', value: 'actions' },
],
footerProps: {
'disable-items-per-page': true,
'items-per-page-options': [],
'items-per-page-all-text': '',
},
PIATConsignments: this.consignmentForm.PIATConsignmentDetails,
editedIndex: -1,
editedItem: modelPIATConsignment,
defaultItem: modelPIATConsignment,
}
},
computed: {
formTitle () { return this.editedIndex === -1 ? 'New' : 'Edit' },
countries() { return this.$store.getters['dict/countries'] },
},
watch: {
dialogEdit (val) { val || this.close() },
dialogDelete (val) { val || this.closeDelete() },
},
methods: {
editItem (item) {
this.editedIndex = this.PIATConsignments.indexOf(item)
this.editedItem = Object.assign({}, item)
this.dialogEdit = true
},
deleteItem (item) {
this.editedIndex = this.PIATConsignments.indexOf(item)
this.editedItem = Object.assign({}, item)
this.dialogDelete = true
},
deleteItemConfirm () {
this.PIATConsignments.splice(this.editedIndex, 1)
this.closeDelete()
},
close () {
this.dialogEdit = false
this.$nextTick(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
})
},
closeDelete () {
this.dialogDelete = false
this.$nextTick(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
})
},
save () {
if (this.editedIndex > -1) {
Object.assign(this.PIATConsignments[this.editedIndex], this.editedItem)
}
else {
this.PIATConsignments.push(this.editedItem)
}
this.close()
},
}
}
</script>
After hour trying.
Yes. Adding #click event on the button is right way.
After this just need manipulate with "current" row data.
methods: {
...
newItem() {
this.$store.dispatch('our-dispatch-method')
.then(res => {
res.id // For example we created element in our api and response is new ID.
// Now push to the table item list editedItem and set the index
this.PIATConsignments.push(this.editedItem)
this.editedIndex = this.PIATConsignments.indexOf(this.editedItem)
// Set your ID from api inside editedItem
this.editedItem.id = res.id;
})
}
...
}

Custom form Validation by Vuetify Showing Error _this.$refs[f].validate is not a function

I want to make custom validation with Vuetify. My Vuetify version is 1.11.3. Here is my template. I set a ref to v-card according to this documentation.
<v-card ref="form">
<v-card-text>
<v-text-field
ref="name"
v-model="name"
label="Full Name:"
:rules="[() => !!name || 'Name is required']"
:error-messages="errorMessages"
required
light
>
</v-text-field>
<v-text-field
ref="email"
v-model="email"
label="Email Address:"
:rules="[
() => !!email || 'Email is required',
() => (!!email && /.+#.+/.test(email)) || 'Email must be valid',
]"
required
light
></v-text-field>
<VuePhoneNumberInput
ref="phone"
v-model="phone"
color="black"
dark-color="white"
size="lg"
default-country-code="BD"
light
required
/>
<v-textarea
ref="msg"
v-model="msg"
label="Message"
:rules="[() => !!msg || 'Message is required']"
light
></v-textarea>
</v-card-text>
<v-card-actions>
<v-btn #click="sendForm"> Submit </v-btn>
</v-card-actions>
</v-card>
I am trying to validate the form and textfields with their references.
This is my code:
data() {
return {
name: null,
email: null,
phone: null,
msg: null,
submitStatus: null,
formHasErrors: false,
errorMessages: '',
}
},
computed: {
form() {
return {
name: this.name,
email: this.email,
phone: this.phone,
msg: this.msg,
}
},
},
watch: {
name() {
this.errorMessages = ''
},
},
methods: {
sendForm() {
this.formHasErrors = false
Object.keys(this.form).forEach((f) => {
if (!this.form[f]) this.formHasErrors = true
this.$refs[f].validate(true)
})
}
When I submit the button, It shows
client.js?06a0:103 TypeError: _this.$refs[f].validate is not a
function
I get following error. What is the wrong with that?
I would recommence using V-form instead of v-card here.
Then you can check if your form is valid with the function this.$ref.myForm.validate() which returns a boolean
Here is a small example:
<v-card>
<v-card-text>
<v-form ref="myForm">
<v-row>
<v-col
cols="12"
sm="7"
>
<v-text-field
prepend-icon="mdi-tag-text"
v-model="form.name"
:rules="[
(v) => !!v || 'Name is requierd',
]"
label="Name"
/>
</v-col>
</v-row>
</v-form>
</v-card-text>
<v-card-actions>
<v-btn
#click="check"
>
Check
</v-btn>
</v-card-actions>
</v-card>
And the script :
export default {
data: () => ({
form:{
name : ""
},
}),
methods:{
check(){
if (this.$refs.myForm.validate()){
//form is valid
} else {
//form is not valid
}
}
}
}

When saving an object using axios post, data from v-select does not persist in the Vuejs database, Vuetify datatable

I have a problem that when using the post method using axes I would need to send the data in the following json format:
{
"id":"1",
"firstName":"Faabio",
"lastName":"Mendes de Jesus",
"phone":"11941649284",
"mobilePhone":"11941649284",
"email":"art7design2013#gmail.com",
"gender":
{"name":"masculino"}
}
However, when saving, sex is stored only in the front, but it is not saved in the database, my console.log records the following situations: In object: config the data is correct and with the association to the gender, but in config: data the association no longer appears indicating that something is missing to persist the data and I'm going crazy about it, follow the console:
This line that was saved, in json format, gender is getting null and data table image with the gender field empty:
{
"id":"3",
"firstName":"ana",
"lastName":"lucia",
"phone":"1188888888",
"mobilePhone":"1188888888",
"email":"analu#gmail.com",
"gender":null
}
My file .vue
<template>
<v-data-table
:headers="headers"
:items="clients"
sort-by="firstName"
class="elevation-2"
>
<template v-slot:top>
<v-toolbar flat color="white">
<v-icon medium>mdi-account-supervisor</v-icon>
<v-toolbar-title> Clients</v-toolbar-title>
<v-divider
class="mx-4"
inset
vertical
></v-divider>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" max-width="600px">
<template v-slot:activator="{ on }">
<v-btn
color="blue"
dark class="mt-6 mb-4"
v-on="on"
rounded
><v-icon medium>mdi-plus</v-icon>Add new</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">{{ formTitle }}</span>
</v-card-title>
<v-card-text>
<v-container>
<v-form>
<v-row>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.firstName" label="First Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.lastName" label="Last Name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.email" label="E-Mail"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.phone" label="Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<v-text-field v-model="editedItem.mobilePhone" label="Mobile Phone"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="12">
<!-- select options-->
<v-select
label='Gender'
v-model='editedItem.gender'
:items='genders'
item-text='name'
return-object
>
</v-select>
</v-col>
</v-row>
</v-form>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="error" rounded #click="close">Cancel</v-btn>
<v-btn color="primary" rounded #click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:item.action="{ item }">
<v-icon
small
color="green"
class="mr-2"
#click="editItem(item)"
>
mdi-pencil
</v-icon>
<v-icon
small
color="red"
#click="deleteItem(item)"
>
mdi-delete
</v-icon>
</template>
<template v-slot:no-data>
<v-btn color="primary" #click="initialize">Reset</v-btn>
</template>
</v-data-table>
</template>
<script>
import axios from 'axios'
import Client from '../../services/clients';
import Gender from '../../services/genders';
export default {
data: () => ({
dialog: false,
headers: [
{
text: 'First Name',
align: 'start',
sortable: false,
value: 'firstName',
},
{ text: 'Last Name', value: 'lastName' },
{ text: 'Email', value: 'email' },
{ text: 'Phone', value: 'phone' },
{ text: 'Mobile Phone', value: 'mobilePhone' },
{ text: 'Gender', value: 'gender.name' },
{ text: 'Actions', value: 'action', sortable: false },
],
clients: [],
genders: [],
errors: [],
editedIndex: -1,
editedItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
defaultItem: {
firstName: '',
lastName: '',
email: '',
phone: '',
mobilePhone: '',
gender: '',
},
}),
computed: {
formTitle () {
return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
},
},
watch: {
dialog (val) {
val || this.close()
},
},
created () {
this.initialize()
},
methods: {
initialize () {
Client.list().then(response => {
this.clients = response.data
}).catch(e => {
console.log(e)
});
Gender.list().then(response => {
this.genders = response.data
}).catch(e => {
console.log(e)
});
},
editItem (item) {
this.editedIndex = this.clients.indexOf(item)
this.editedItem = Object.assign({}, item)
this.editedID = this.editedItem.id
this.dialog = true
axios.put('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
this.response = response
}).catch(error => {
console.log(error.response)
});
},
deleteItem (item) {
if (confirm("Do you really want to delete?")) {
const index = this.clients.indexOf(item)
this.deletedItem = Object.assign({}, item)
this.deletedID = this.deletedItem.id
this.clients.splice(index, 1);
axios.delete('http://192.168.26.130:3000/client/' + item.id)
.then(response => {
this.response = response
}).catch(error => {
console.log(error.response)
});
}
},
close () {
this.dialog = false
setTimeout(() => {
this.editedItem = Object.assign({}, this.defaultItem)
this.editedIndex = -1
}, 300)
},
save () {
if (this.editedIndex > -1) {
Object.assign(this.clients[this.editedIndex], this.editedItem)
} else {
this.clients.push(this.editedItem)
axios.post('http://192.168.26.130:3000/client/', this.editedItem)
.then(response => {
console.log(response)
}).catch(error => {
console.log(error.response)
});
}
this.close()
},
},
}
</script>
The code snippet that saves to the database:
this.clients.push(this.editedItem)
axios.post('http://192.168.26.130:3000/client/', this.editedItem)
.then(response => {
console.log(response)
}).catch(error => {
console.log(error.response)
Could someone save me? Thank you very much in advance .

v-checkboxes are true when the page is loaded

I am doing form validation in vuelidate. I have a group of checkboxes. There are other fields also in the form. I want to show the success-messages of the checkbox only when a checkbox is checked. But the success-messages are showing when the page loads even when the checkboxes are not checked. What is wrong with my code. Here is my code:
<template>
<v-dialog persistent max-width="75%" v-model="dialog">
<v-btn round color="secondary" slot="activator">New Enquiry</v-btn>
<v-card>
<v-card-text>
<v-form ref="enquiryForm">
<v-container fluid grid-list-lg>
<v-layout row wrap>
<v-flex xs6>
<span class="title">Prefrences</span>
<v-layout row wrap>
<v-flex xs6>
<v-checkbox
v-for="( value, index ) in preferencesData"
:key="index"
v-model="enquiryForm.preferences"
:label="value"
:value="index"
required
:success="!!enquiryForm.preferences"
:success-messages="(!!enquiryForm.preferences) ? 'Ok' : ''"
:error-messages="fieldErrors('enquiryForm.preferences')"
#change="$v.enquiryForm.preferences.$touch()"
#blur="$v.enquiryForm.preferences.$touch()"
></v-checkbox>
</v-flex>
</v-layout>
</v-flex>
</v-layout>
</v-container>
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn flat lg color="secondary" #click="close">Cancel</v-btn>
<v-btn flat lg color="secondary" #click="submit">Submit</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
<script>
import { mapGetters } from 'vuex';
import validationMixin from '#/mixins/validationMixin';
import { required } from 'vuelidate/lib/validators'
import moment from 'moment';
const defaultFormData = {
date: moment().format(),
preferences: []
}
export default {
mixins: [ validationMixin ],
validations: {
enquiryForm: {
preferences: { required }
}
},
validationMessages: {
enquiryForm: {
preferences: {
required: 'Please select at least one.'
}
}
},
data ( ) {
return {
dialog: false,
enquiryForm: Object.assign({}, defaultFormData),
preferencesData: [
'Furnished',
'SemiFurnished',
'UnFurnished'
]
}
},
computed: {
...mapGetters({
projects: 'projects',
cpexecutives: 'cpexecutives'
})
},
mounted ( ) {
this.$store.dispatch('fetchProjects');
this.$store.dispatch('getUsers');
this.clearForm();
},
methods: {
clearForm ( ) {
this.$v.$reset();
this.enquiryForm = Object.assign({}, defaultFormData);
},
submit ( ) {
this.$v.$touch()
this.$store.dispatch('addEnquiry', {
date: moment().format(),
preferences: this.enquiryForm.preferences
})
.then( ( result ) => {
this.close();
} )
.catch( ( err ) => {
console.log( err );
} )
},
close ( ) {
this.clearForm();
this.dialog = false;
}
}
}
</script>
I tried:
:success="!!enquiryForm.preferences && !$invalid"
:success-messages="(!!enquiryForm.preferences && !$invalid) ? 'Ok' : ''"
and
:success="!!enquiryForm.preferences && !$v.$invalid"
:success-messages="(!!enquiryForm.preferences && !$v.$invalid) ? 'Ok' : ''"
but that did not help.
I managed to do it
:success="!!enquiryForm.preferences && !$v.enquiryForm.preferences.$invalid"
:success-messages="(!!enquiryForm.preferences && !$v.enquiryForm.preferences.$invalid) ? 'Ok' : ''"
But, I want to show the success message 'Ok' and error message only once below the checkboxes. How do I do it?