In use vee-validate library vue-select element does not work - validation

In vuejs 2.5 application I use vee-validate library ( http://vee-validate.logaretm.com/examples.html ) with inputs like:
<template>
<section>
<form #submit.prevent="validateBeforeSubmit" class="form-horizontal">
<div class="row">
<div class="col-xs-12 form-group">
<label class="col-xs-12 col-sm-4 control-label" for="name">Name<span class="required"> * </span></label>
<div class="col-xs-12 col-sm-8">
<input v-validate="'required|max:255'" v-model="task.name"
:class="{ 'form-control':true, 'input': true, 'text-danger': vueErrorsList.has('name') }" name="name" id="name"
type="text" placeholder="name">
<span v-show="vueErrorsList.has('name')" class="text-danger">{{ vueErrorsList.first('name') }}</span>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
<label class="col-xs-12 col-sm-4 control-label" for="category_id">Category<span class="required"> * </span></label>
<div class="col-xs-12 col-sm-8">
<v-select v-validate="'required'" id="category_id" name="category_id" v-model="selection_category_id" :options=categoriesSelectionList></v-select>
<span v-show="vueErrorsList.has('category_id')" class="text-danger">{{ vueErrorsList.first('category_id') }}</span>
</div>
</div>
</div>
</form>
</section>
</template>
<script>
import {bus} from '../../app';
...
import vSelect from 'vue-select'
Vue.component('v-select', vSelect) // https://github.com/sagalbot/vue-select
import moment from 'moment';
...
export default {
created() {
this.$validator.localize(window.CURRENT_LANG, {
attributes: {
selection_category_id: 'category',
category_id: 'category',
...
}
});
}, // created() {
...
methods
:
{
validateBeforeSubmit()
{
this.$validator.validateAll().then((result) => {
if (result) {
this.updateTask()
} else {
this.showPopupMessage('Error ' + ( this.is_insert ? 'adding' : 'updating' ) + ' task. ', 'error');
}
})
}
, // validateBeforeSubmit() {
updateTask()
{
...
}
, // updateTask() {
} // methods:{
}
</script>
But In the code above the second input is vue-select element ( https://github.com/sagalbot/vue-select )
and it is not validated at all.
I put
v-validate="'required'"
to it but it does not work.
I use validateBeforeSubmit method and if there is a way to make some kind of custom validation, so I would check is value in
select input is selected?
Thank you!

Related

How to set if else statement in script

-I wan't to do like this: if(!editMode) show create page else show update page because my create and edit using same form so I combine it.
-I'm learning online tutorial but the tutorial show create and edit seperately.
-Please Helps and Thank/.\
<template>
<div v-if="!edit">
<h1>Create Post</h1>
<form #submit.prevent="addPost">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Post Title:</label>
<input type="text" class="form-control" v-model="post.title">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Post Body:</label>
<textarea class="form-control" v-model="post.body" rows="5"></textarea>
</div>
</div>
</div><br />
<div class="form-group">
<button class="btn btn-primary">Create</button>
</div>
</form>
</div>
<div v-else>
<h1>Update Post</h1>
<form #submit.prevent="updatePost">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Post Title:</label>
<input type="text" class="form-control" v-model="post.title">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Post Body:</label>
<textarea class="form-control" v-model="post.body" rows="5"></textarea>
</div>
</div>
</div><br />
<div class="form-group">
<button class="btn btn-primary">Update</button>
</div>
</form>
</div>
</template>
<script>
export default {
data(){
return {
edit:false,
post:{}
}
},
},
//this is for create
methods: {
addPost(){
let uri = 'http://localhost:8000/post/create';
this.axios.post(uri, this.post).then((response) => {
this.$router.push({name: 'posts'});
});
},
},
//this is for get data before update
created() {
let uri = `http://localhost:8000/post/edit/${this.$route.params.id}`;
this.axios.get(uri).then((response) => {
this.post = response.data;
});
},
//this is for update post
updatePost() {
let uri = `http://localhost:8000/post/update/${this.$route.params.id}`;
this.axios.post(uri, this.post).then((response) => {
this.$router.push({name: 'posts'});
});
}
</script>
**I also set this in my app.js**
{
name: 'create',
path: '/create',
component: PostForm,
props: {editMode: false}
},
{
name: 'edit',
path: '/edit/:id',
component: PostForm,
props: {editMode: true}
}
My Error--->when I press edit btn show create page and using addPost function.
Result--> how to use if else to solve this.... Sorry I'm rookie in programming.
I believe you can simply have the js figure out the add or update.
<template>
<div>
<h1 v-if="!edit">Create Post</h1>
<h1 v-else>Update Post</h1>
<form #submit.prevent="postSomething">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Post Title:</label>
<input type="text" class="form-control" v-model="post.title">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Post Body:</label>
<textarea class="form-control" v-model="post.body" rows="5"></textarea>
</div>
</div>
</div><br />
<div class="form-group">
<button v-if="!edit" class="btn btn-primary">Create</button>
<button v-else class="btn btn-primary">Update</button>
</div>
</form>
</div>
</template>
and the js:
methods: {
postSomething(){
if(!this.edit){
this.addPost()
}else{
this.updatePost()
}
},
addPost(){
console.log('should add')
},
updatePost(){
console.log('should update')
}
}

reactive form is not valid even though entering the values in fields in angular 6

I have login reactive form and added validations to form fields.
Once the user entered the value in form fields it should update the form value on the screen as I'm displaying form.value and also it should display the form status too. Both form value and status are not changing.
Nothing happened.
html
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<h2>Login</h2>
<form class='form' [formGroup]='loginForm' (ngSubmit)='onSubmit()' novalidate>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Username"/>
<div *ngIf="f.username.touched && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">Username is required</div>
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password"/>
<div *ngIf="f.password.touched && f.password.errors" class="invalid-feedback">
<div *ngIf="f.password.errors.required">Password is required</div>
</div>
</div>
<div class="form-group">
<label><input type="checkbox"> Remember me</label>
</div>
<div class="form-group">
<button [disabled]="loginForm.invalid" class="btn btn-primary">Login</button>
</div>
</form>
<p>Form value: {{loginForm.value | json}}</p>
<p>Form value: {{loginForm.status | json}}</p>
</div>
</div>
</div>
</div>
ts file
import { ObjectMethod } from '#babel/types/lib';
import { AuthService } from './../auth.service';
import { Component, OnInit } from '#angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '#angular/forms';
#Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;
returnUrl: string;
constructor(
private formBuilder: FormBuilder,
private authService: AuthService) { }
ngOnInit() {
this.loginForm = this.formBuilder.group({
username: ['', [ Validators.required]],
password: ['', [ Validators.required]]
});
}
//convenience getter for easy access to form fields
get f() {
return this.loginForm.controls;
}
onSubmit() {
console.log(this.loginForm.value);
/*Object.keys(this.loginForm.value).map(e => {
console.log(">>>>>>>",this.loginForm.value[e]);
});*/
this.authService.getUserDetails();
}
}
Because of form is invalid, login button is also not enabled.
Please help me in solving this.
You need to add formControlName to your inputs such as:
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" formControlName="username" placeholder="Username"/>
<div *ngIf="f.username.touched && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">Username is required</div>
</div>
</div>
For validity check I would use loginform.valid, this should return false when the form is invalid

Reset input on click, vue

I would like to have my text fields empty when I click "add task" button. How could I achieve that?
<template>
<div class="col-md-6" >
<div class="card bg-light mt-4">
<div class="card-header">Task Form</div>
<div class="card-body">
<form action="./api/word" method="post" #submit.prevent="addTask()">
<div class="form-group">
<input type="text" name="title" v-model="title" placeholder="Local word" class="form-control">
</div>
<div class="form-group">
<input type="text" name="second_title" v-model="second_title" placeholder="Foreign word" class="form-control">
</div>
<div class="form-group">
<input type="submit" value="Add Task" class="btn btn-info">
</div>
</form>
</div>
</div>
</div>
</template>
export default {
data() {
return {
title: '',
second_title: ''
}
},
mounted() {
console.log('Component mounted.')
},
methods: {
addTask() {
Event.$emit('taskCreated', {
title: this.title,
second_title: this.second_title
});
axios.post('./api/word', {
title: this.title,
second_title: this.second_title,
})
this.title = '';
this.second_title = '';
}
It's very simple, in the addTask() just add event as parameter and in the end of the function you write: event.target.reset();
Code:
<template>
<div class="col-md-6" >
<div class="card bg-light mt-4">
<div class="card-header">Task Form</div>
<div class="card-body">
<form action="./api/word" method="post" #submit.prevent="addTask">
<div class="form-group">
<input type="text" name="title" v-model="title" placeholder="Local word" class="form-control">
</div>
<div class="form-group">
<input type="text" name="second_title" v-model="second_title" placeholder="Foreign word" class="form-control">
</div>
<div class="form-group">
<input type="submit" value="Add Task" class="btn btn-info">
</div>
</form>
</div>
</div>
</div>
</template>
And there:
export default {
data() {
return {
title: '',
second_title: ''
}
},
mounted() {
console.log('Component mounted.')
},
methods: {
addTask(event) {
Event.$emit('taskCreated', {
title: this.title,
second_title: this.second_title
});
axios.post('./api/word', {
title: this.title,
second_title: this.second_title,
})
this.title = '';
this.second_title = '';
event.target.reset();
}
Hope it works for you ; )

cannot insert data into vue select sent from laravel

This is the vuejs component for editing song info. the problem here is with tags.I cannot show the tags of the song in vue select for editing.
<template>
<div>
<a data-toggle="modal" :data-target="'#EditModal'+ modalid" #click="select(song)"><span title="edit" class="glyphicon glyphicon-pencil" ></span></a>
<a class=""><span title="delete" class="glyphicon glyphicon-trash"></span></a>
<!-- Modal for editing song tracks-->
<div class="modal fade" :id="'EditModal'+ modalid" tabindex="-1" role="dialog" aria-labelledby="EditModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="EditModalLabel">Edit Song</h5>
<button type="button" class="close" ref="closemodal" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form ref="uploadform">
<div class="form-group">
<div class="row">
<div class="col-md-12">
<div class="col-md-5">
<button type="button" #click="browseImage" class="btn btn-md btn-default">Choose image:</button>
<div id="image_previews">
<img ref='image' class="" v-bind:src="image" width="200px" height="200px" >
<input class="form-control-file" ref="imageinput" type="file" name="feature_image" #change="showImage($event)">
</div>
</div>
<div class="col-md-7">
<div class="form-group">
<label for="title">Song Title:</label>
<input type="text" v-model="esong.title" class="form-control" required maxlength="255">
</div>
<div class="form-group">
<label for="genre"> Genre (tag atleast one) </label>
<v-select :placeholder="'choose tag'" v-model="tagids" label="name" multiple :options="tags"></v-select>
</div>
<div class="form-group">
<label for="upload_type">Song Upload Type</label>
<select name="upload_type" v-model="esong.upload_type" class="form-control">
<option value="public">public( free )</option>
<option value="private">private( for sale )</option>
</select>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Description:</label>
<textarea class="form-control" id="message-text" v-model="esong.song_description"></textarea>
</div>
<div class="form-group" v-if="private">
<label for="upload_type">Song price</label>
<input type="text" v-model="esong.amount" class="form-control" required maxlength="255">
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" #click="edit">Save</button>
</div>
</div>
</div>
</div><!-- end of modal -->
</div>
</template>
<script>
import vSelect from 'vue-select'
export default {
props: ['song','modalid','index','tags'],
components: {vSelect},
mounted() {
},
watch: {
tagids() {
console.log('changed tagids value');
// this.value = this.tagsid;
}
},
computed: {
private() {
if(this.esong.upload_type == 'private') {
return true;
}
return false;
},
},
methods: {
select(song) {
console.log(song.title);
this.getTagIds(song);
},
edit() {
let formData = new FormData();
formData.append('title', this.esong.title);
formData.append('img', this.esong.img);
formData.append('description', this.esong.song_description);
formData.append('upload_type', this.esong.upload_type);
formData.append('amount', this.esong.amount);
formData.append('tags', JSON.stringify(this.tagids));
formData.append('_method', 'PUT');
axios.post('/artist/songs/' + this.esong.id, formData,{
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(response =>{
this.$refs.closemodal.click();
toastr.success('successfully edited song.');
this.$emit('update', {song:this.esong,index:this.index});
}).catch(error => {
console.log(error);
});
},
getTagIds(song) {
axios.post('/gettagids', song ).then(response =>{
this.tagids = response.data;
}).catch(error =>{
console.log(error);
});
},
browseImage() {
this.$refs.imageinput.click();
},
showImage(event) {
this.esong.img = event.target.files[0];
this.image = URL.createObjectURL(event.target.files[0]);
}
},
data() {
return {
esong: this.song,
tagids: {id:'', name:'', label:''},
name:'name',
image:this.song.image
}
}
}
</script>
<style scoped>
input[type="file"] {
display: none;
}
#image_previews {
border-radius: 5px;background-color: whitesmoke; width: 200px; height: 200px;
}
.btn{
border-radius: 0px;
}
</style>
here I cannot get the selected value that was inserted in my table. I wanted to show the tagged values for a song. I am able to get all object of tagged songs from axios post request but v-select doesn't shows the selected value retrieved from a table.
the object received from laravel is similar to the object provided in options which work well with v-select..but it doesn't show the same structure object provided to v-model..or should I provide other props. the document for vue select has not mentioned any of these things

Vuejs components not getting displayed

I've a password_reset.vue script as below:
<template>
<form>
<div class="alert alert-danger" v-if="form.errors.has('photo')">
#{{ form.errors.get('photo') }}
</div>
<div class="row">
<div class="col-md-3">
<div class="label">Current Password</div>
</div>
<div class="col-md-9">
<div class="field--wrapper">
<label for="current_password">Current Password</label>
<input class="nom-fields" type="password"
v-model="form.current_password"
id="current_password" name="current_password"
placeholder="Current Password" value="">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="label">Repeat Password</div>
</div>
<div class="col-md-9">
<div class="field--wrapper">
<label for="password_repeat">Repeat Password</label>
<input class="nom-fields" type="password"
v-model="form.password_repeat"
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-9 text-right">
<button id="change_password"
class="blue-btn" v-on:click="update">
Change Password
</button>
</div>
</div>
</form>
</template>
<script>
export default{
data(){
return{
form: new MyForm({
current_password: '',
new_password: '',
password_repeat: '',
message: ''
})
};
},
methods: {
/**
* Update the user's password.
*/
update(e) {
e.preventDefault();
this.form.startProcessing();
this.$http.post('/account/change_password', this.form)
.then(function(response) {
this.form.finishProcessing();
})
.catch(function(response) {
this.form.setErrors(response.data);
});
},
}
}
</script>
and in my bootstrap.js file, I've:
import './interceptors.js';
import './forms/form.js';
import './forms/errors.js';
import PasswordReset from './settings/password_reset.vue'
new Vue({
el: 'body',
components: {
PasswordReset
},
ready() {
}
});
and in the app.js file I've:
import './bootstrap.js'
HTML:
<body>
<password-reset></password-reset>
</body>
But it seems the component is not getting displayed. I tried to alert a msg in the ready method and it's working fine. I'm loading Vue.js also.

Resources