#vue.js
Вопрос:
Здесь я пытаюсь проверить схему генератора форм vue. Как и в компоненте, я объявил компонент «schema_third». Таким образом, эти поля ввода «schema_third, связанные», объявляются в vue.js сценарий. Я пытаюсь сделать так, чтобы, когда пользователь нажимает кнопку «далее», но не заполняет никаких полей ввода, кнопка не должна быть доступна для нажатия, пока все поля не будут полностью заполнены. Здесь я написал метод «checkform» для проверки этого, но я все еще не могу проверить. Я не могу понять, что я делаю не так. Пожалуйста, если кто-нибудь может помочь мне решить эту проблему.
<template>
<div>
<vue-form-g
:schema="schema_third"
:model="model"
:options="formOptions"
></vue-form-g>
<span class="prev_next">
<button class="prev_next_btn" @click="prev()">Previous</button>
<button class="prev_next_btn" @click="next()">Next</button>
</span>
</div>
</template>
<script>
new Vue({
el: '#q-vikreya',
components: {
"vue-form-g": VueFormGenerator.component
},
data() {
return {
step: 1,
formKey: 1,
model: {
job_title: null,
Experience: null,
Location: null,
Industry: null,
Time: null,
},
schema_third: {
fields: [
{
type: "input",
inputType: "text",
placeholder: "Job title",
required: true,
model: "job_title",
name: "Job_title",
styleClasses: ["half-width col-xs-12 col-sm-6", "job_title"],
validator: VueFormGenerator.validators.text,
},
{
type: "input",
inputType: "text",
placeholder: "Experience",
required: true,
model: "Experience",
styleClasses: ["half-width col-xs-12 col-sm-6", "Experience"],
validator: VueFormGenerator.validators.text,
},
{
type: "input",
inputType: "text",
placeholder: "Location",
required: true,
model: "Location",
styleClasses: ["half-width col-xs-12 col-sm-6", "job_title"],
validator: VueFormGenerator.validators.text,
},
{
type: "input",
inputType: "text",
placeholder: "Industry",
required: true,
model: "Industry",
styleClasses: ["half-width col-xs-12 col-sm-6", "Experience"],
validator: VueFormGenerator.validators.text,
},
{
type: "input",
inputType: "text",
placeholder: "Time",
required: true,
model: "Time",
styleClasses: ["half-width col-xs-12 col-sm-6", "job_title"],
validator: VueFormGenerator.validators.text,
},
{
type: "input",
inputType: "text",
placeholder: "Time",
required: true,
model: "Time",
styleClasses: ["half-width col-xs-12 col-sm-6", "Experience"],
validator: VueFormGenerator.validators.text,
},
],
},
formOptions: {
validateAfterLoad: true,
validateAfterChanged: true,
},
};
},
delimiters: ["<%", "%>"],
ready: function () {
console.log("ready");
},
methods: {
prev() {
if (this.checkForm()) {
this.step--;
}
},
next() {
if (this.checkForm()) {
this.step ;
}
},
checkForm() {
let fields = Object.assign([], this.schema_third.fields);
let errors = [];
fields.forEach((field, index) => {
if (field.required amp;amp; !this.model[field.model]) {
fields[index].styleClasses.push("error-field");
errors.push(field.placeholder " is required");
} else if (
this.model[field.model] amp;amp;
fields[index].styleClasses.includes("error-field")
) {
let indexOfErrorClass = fields[index].styleClasses.indexOf(
"error-field"
);
fields[index].styleClasses.splice(indexOfErrorClass, 1);
}
});
this.schema_third.fields = fields;
return errors.length === 0;
},
submitForm: function () {
axios({
method: "POST",
url: "{% url 'PostAd' %}", //django path name
headers: {
"X-CSRFTOKEN": "{{ csrf_token }}",
"Content-Type": "application/json",
},
data: {
category: this.category,
title: this.title,
address: this.address,
city: this.city,
state: this.state,
zip: this.zip,
price: this.price,
description: this.description,
radio_price: this.radio_price,
Job_title: this.model,
}, //data
})
.then((response) => {
console.log("response");
console.log(response.data);
this.success_msg = response.data["msg"];
window.location.replace('{% url "classifieds" %}'); // Replace home by the name of your home view
})
.catch((err) => {
this.err_msg = err.response.data["err"];
console.log("response1");
console.log(err.response.data);
});
},
},
};
</script>
<style>
.error-field input {
border: solid thin red;
}
</style>
</script>