P-133 Валидация полей

This commit is contained in:
Denzel committed 2025-01-24 12:03:17 +03:00
1 parent 2ef1572788
commit f4573f0d43
1 file changed
+135 -114
+135 -114
View File
@@ -14,10 +14,8 @@
</v-card> </v-card>
</template> </template>
<template v-else> <template v-else>
<div class="v-col flex-grow-1 flex-shrink-0 "> <div class="v-col flex-grow-1 flex-shrink-0 ">
<v-stepper v-model="step" class="d-flex flex-column fill-height" editable>
<v-stepper v-model="step" editable class="d-flex flex-column fill-height">
<v-stepper-header class="flex-grow-0 flex-shrink-1"> <v-stepper-header class="flex-grow-0 flex-shrink-1">
<v-stepper-item :complete="step > 0" step="1">Основная информация <v-stepper-item :complete="step > 0" step="1">Основная информация
@@ -30,12 +28,12 @@
<v-stepper-window class="fill-height"> <v-stepper-window class="fill-height">
<!-- Основная информация --> <!-- Основная информация -->
<v-stepper-window-item step="1" class="fill-height"> <v-stepper-window-item class="fill-height" step="1">
<v-card flat class="fill-height d-flex flex-column "> <v-card class="fill-height d-flex flex-column " flat>
<v-card-title>Основная информация</v-card-title> <v-card-title>Основная информация</v-card-title>
<v-card-text class="d-flex flex-column overflow-auto"> <v-card-text class="d-flex flex-column overflow-auto">
<v-text-field v-model="form.title" :rules="[rules.required]" label="Название голосования" <v-text-field v-model="form.title" :rules="[rules.required]" class="flex-grow-0 flex-shrink-1"
class="flex-grow-0 flex-shrink-1" label="Название голосования"
variant="underlined"></v-text-field> variant="underlined"></v-text-field>
<div class="flex-grow-1 flex-shrink-0"> <div class="flex-grow-1 flex-shrink-0">
@@ -50,20 +48,21 @@
<div class="d-flex ga-3 align-center"> <div class="d-flex ga-3 align-center">
<h6 class="text-subtitle-1 pt-2">начало:</h6> <h6 class="text-subtitle-1 pt-2">начало:</h6>
<date-select :datedefault="form.startDate" @returndate="returnstart"/> <date-select :datedefault="form.startDate" @returndate="returnstart"/>
<v-select v-model="selectedTimeStart" :items="timeOptions" density="compact" variant="plain" <v-select v-model="selectedTimeStart" :items="timeOptions" density="compact" hide-details
hide-details> variant="plain">
<template v-slot:prepend-inner> <template v-slot:prepend-inner>
<v-icon icon="mdi-timer" size="xsmall" color="red"></v-icon> <v-icon color="red" icon="mdi-timer" size="xsmall"></v-icon>
</template> </template>
</v-select> </v-select>
</div> </div>
<div class="d-flex ga-3 align-center"> <div class="d-flex ga-3 align-center">
<h6 class="text-subtitle-1 pt-2">окончание:</h6> <h6 class="text-subtitle-1 pt-2">окончание:</h6>
<date-select :datedefault="form.endDate" @returndate="returnend"/> <date-select :datedefault="form.endDate" @returndate="returnend"/>
<v-select v-model="selectedTimeEnd" :items="timeOptions" density="compact" outlined variant="plain" <v-select v-model="selectedTimeEnd" :items="timeOptions" density="compact" hide-details
hide-details> outlined
variant="plain">
<template v-slot:prepend-inner> <template v-slot:prepend-inner>
<v-icon icon="mdi-timer" size="xsmall" color="red"></v-icon> <v-icon color="red" icon="mdi-timer" size="xsmall"></v-icon>
</template> </template>
</v-select> </v-select>
</div> </div>
@@ -78,48 +77,58 @@
</v-stepper-window-item> </v-stepper-window-item>
<!-- Создание опроса --> <!-- Создание опроса -->
<v-stepper-window-item step="2" class="fill-height"> <v-stepper-window-item class="fill-height" step="2">
<v-card flat class="fill-height d-flex flex-column"> <v-card class="fill-height d-flex flex-column" flat>
<v-card-title>Создание опроса:</v-card-title> <v-card-title>Создание опроса:</v-card-title>
<v-card-text style="max-height: 62vh; overflow-y: auto;"> <v-card-text style="max-height: 62vh; overflow-y: auto;">
<transition-group name="list" tag="div"> <transition-group name="list" tag="div">
<div v-for="(question, index) in form.questions" :key="index" class="mb-4"> <div v-for="(question, index) in form.questions" :key="index" class="mb-4">
<div class="d-flex"> <div class="d-flex">
<v-text-field density="comfortable" v-model="question.text" :label="'Вопрос ' + (index + 1)" <!-- <v-text-field v-model="question.text" :label="'Вопрос ' + (index + 1)" :rules="[rules.required]"-->
:rules="[rules.required]" variant="underlined"> <!-- density="comfortable" variant="underlined">-->
<v-text-field
v-model="question.text"
:label="'Вопрос ' + (index + 1)"
density="comfortable"
variant="underlined"
:rules="[rules.required]"
@input="updateQuest(index)"
>
<template v-slot:append> <template v-slot:append>
<v-btn color="error" icon="mdi-delete" size="small" @click="removeQuestion(index)"/> <v-btn color="error" icon="mdi-delete" size="small" @click="removeQuestion(index)"/>
</template> </template>
</v-text-field> </v-text-field>
</div> </div>
<v-file-input density="comfortable" v-model="question.image" accept="image/*" <v-file-input v-model="question.image" accept="image/*" density="comfortable"
label="Картинка для вопроса" variant="outlined"></v-file-input> label="Картинка для вопроса" variant="outlined"></v-file-input>
<v-divider></v-divider> <v-divider></v-divider>
<div class="py-2 text-overline text-grey">Настройки вопроса</div> <div class="py-2 text-overline text-grey">Настройки вопроса</div>
<div class="v-row "> <div class="v-row ">
<v-col cols="auto" class="align-self-center font-weight-bold py-0"> <v-col class="align-self-center font-weight-bold py-0" cols="auto">
Тип ответа Тип ответа
</v-col> </v-col>
<v-col cols="3" class="py-0 align-self-center "> <v-col class="py-0 align-self-center " cols="3">
<v-select density="compact" v-model="question.settings.choice" :items="questionTypes" <v-select v-model="question.settings.choice" :items="questionTypes" :list-props="{density:'compact'}"
@update:model-value="question.answers=[]" density="compact"
:list-props="{density:'compact'}" hide-details
item-title="name" item-value="choice" label="" hide-details variant="outlined"></v-select> item-title="name" item-value="choice" label="" variant="outlined"
@update:model-value="question.answers=[]"></v-select>
<!-- choice --> <!-- choice -->
</v-col> </v-col>
<v-spacer/> <v-spacer/>
<v-col cols="auto" class="align-self-center font-weight-bold"> <v-col class="align-self-center font-weight-bold" cols="auto">
Число возможных ответов Число возможных ответов
</v-col> </v-col>
<v-col cols="1" class=""> <v-col class="" cols="1">
<v-text-field density="compact" v-model="question.settings.multiChoice" <v-text-field v-model="question.settings.multiChoice" :rules="[rules.required && question.settings.multiChoice > 0]"
:rules="[rules.required && question.settings.multiChoice > 0]" hide-details type="number" density="compact" hide-details
min="1" variant="outlined"> min="1"
type="number" variant="outlined">
<template v-slot:prepend> <template v-slot:prepend>
</template> </template>
@@ -133,27 +142,23 @@
<div v-if="question.settings.choice === 'free'"> <div v-if="question.settings.choice === 'free'">
<transition-group name="list" tag="div"> <transition-group name="list" tag="div">
<div v-for="(answer, ind) in question.answers" :key="ind" class="d-flex align-start mb-4"> <div v-for="(answer, ind) in question.answers" :key="ind" class="d-flex align-start mb-4">
<v-text-field v-model="answer.text" :label="'Ответ ' + (ind + 1)" :rules="[rules.required]" <v-text-field v-model="answer.text" :label="'Ответ ' + (ind + 1)"
class="mr-4" variant="outlined" density="compact"></v-text-field> :rules="[rules.required]"
<v-file-input v-model="answer.image" accept="image/*" dense hide-details label="Картинка" class="mr-4" density="compact" variant="outlined"></v-text-field>
variant="outlined" density="compact"></v-file-input> <v-file-input v-model="answer.image" accept="image/*" dense density="compact" hide-details
<v-btn class="ms-1" color="error" icon="mdi-delete" size="small" @click="removeAnswer(index,ind)"> label="Картинка" variant="outlined"></v-file-input>
<v-btn class="ms-1" color="error" icon="mdi-delete" size="small"
@click="removeAnswer(index,ind)">
</v-btn> </v-btn>
</div> </div>
</transition-group> </transition-group>
<div class="d-flex justify-start"> <div class="d-flex justify-start">
<v-btn size="small" color="teal-darken-4" @click="addAnswer(index)">Добавить ответ</v-btn> <v-btn color="teal-darken-4" size="small" @click="addAnswer(index)">Добавить ответ</v-btn>
</div> </div>
</div> </div>
<div v-else> <div v-else>
<div class="v-row"> <div class="v-row">
<v-radio-group v-model="question.settings.onlyDep" class="text-caption" inline>
<v-radio-group class="text-caption" inline v-model="question.settings.onlyDep">
<template v-slot:label> <template v-slot:label>
<div class="text-body-2 font-weight-bold">Голосование по отделам:</div> <div class="text-body-2 font-weight-bold">Голосование по отделам:</div>
</template> </template>
@@ -163,87 +168,69 @@
</template> </template>
</v-radio> </v-radio>
</v-radio-group> </v-radio-group>
</div> </div>
<v-autocomplete <v-autocomplete
class=" text-no-wrap" v-model="question.selectedEmployees"
v-model="question.selectedEmployees" :items="allEmployeesgroupedbydep" :items="allEmployeesgroupedbydep" class=" text-no-wrap"
item-title="fio" item-value="id" label="Выберите сотрудников" multiple variant="outlined" density="compact" item-title="fio" item-value="id" label="Выберите сотрудников" multiple
density="compact" variant="outlined"
@update:model-value="(selected)=>question.answers=question.answers.filter(e=>selected.includes(e.text))" @update:model-value="(selected)=>question.answers=question.answers.filter(e=>selected.includes(e.text))"
> >
<template #prepend-item> <template #prepend-item>
<v-checkbox class="pa-0" :model-value="question.selectedEmployees.length==allEmployees.length" label="Все" <v-checkbox :model-value="question.selectedEmployees.length===allEmployees.length"
@update:model-value="handleSelectAll(question.selectedEmployees.length==allEmployees.length, index)"></v-checkbox> class="pa-0"
label="Все"
@update:model-value="handleSelectAll(question.selectedEmployees.length===allEmployees.length, index)"></v-checkbox>
</template> </template>
<template #selection="{item}"> <template #selection="{item}">
<v-chip <v-chip
:prepend-avatar="checkremote(`${spravURL}${item.raw.login}.jpg`)" :prepend-avatar="checkremote(`${spravURL}${item.raw.login}.jpg`)"
> >
{{ item.raw.familyname }}</v-chip> {{ item.raw.familyname }}
</v-chip>
</template> </template>
<template #item="data"> <template #item="data">
<template v-if="typeof data.item.raw !== 'object'"> <template v-if="typeof data.item.raw !== 'object'">
<v-divider/> <v-divider/>
<v-list-group class='pa-4 text-overline text-grey' v-text="data.item.raw"></v-list-group> <v-list-group class='pa-4 text-overline text-grey'
v-text="data.item.raw"></v-list-group>
</template> </template>
<template v-else> <template v-else>
<v-list-item <v-list-item
slim
density="compact"
:append-avatar="checkremote(`${spravURL}${data.item.raw.login}.jpg`)" :append-avatar="checkremote(`${spravURL}${data.item.raw.login}.jpg`)"
density="compact"
slim
@click="onEmployeesChange(data.item.raw.id, index)" @click="onEmployeesChange(data.item.raw.id, index)"
> >
<template #prepend> <template #prepend>
<v-checkbox hide-details <v-checkbox :model-value="question.selectedEmployees.includes(data.item.raw.id)"
:model-value="question.selectedEmployees.includes(data.item.raw.id)" density="compact"
density="compact"></v-checkbox> hide-details></v-checkbox>
</template> </template>
<v-list-item-title class="text-third" v-text="data.item.raw.fio"></v-list-item-title> <v-list-item-title class="text-third" v-text="data.item.raw.fio"></v-list-item-title>
</v-list-item> </v-list-item>
</template> </template>
</template> </template>
</v-autocomplete> </v-autocomplete>
<transition-group name="list" tag="div"> <transition-group name="list" tag="div">
<div v-for="(person, ind) in question.answers" :key="person" class="v-row align-center mb-4"> <div v-for="(person, ind) in question.answers" :key="person"
<v-col cols="7" class="py-0" > class="v-row align-center mb-4">
<v-col class="py-0" cols="7">
<v-chip :prepend-avatar="checkremote(`${spravURL}${findEmployeeById(person.text)?.login}.jpg`)" :text="findEmployeeById(person.text)?.fio "> <v-chip
:prepend-avatar="checkremote(`${spravURL}${findEmployeeById(person.text)?.login}.jpg`)"
:text="findEmployeeById(person.text)?.fio ">
</v-chip> </v-chip>
</v-col> </v-col>
<v-col class="py-0 d-flex" cols="5"> <v-col class="py-0 d-flex" cols="5">
<v-file-input v-model="person.image" accept="image/*" dense hide-details label="Картинка" <v-file-input v-model="person.image" accept="image/*" dense density="compact"
variant="outlined" density="compact"></v-file-input> hide-details
label="Картинка" variant="outlined"></v-file-input>
<v-btn class="ms-1" color="error" icon="mdi-delete" size="small" <v-btn class="ms-1" color="error" icon="mdi-delete" size="small"
@click="question.selectedEmployees.splice(ind,1); removeAnswer(index,ind)"> @click="question.selectedEmployees.splice(ind,1); removeAnswer(index,ind)">
</v-btn> </v-btn>
</v-col> </v-col>
</div> </div>
@@ -254,8 +241,7 @@
</transition-group> </transition-group>
<v-divider class="ma-2 pa-1"></v-divider> <v-divider class="ma-2 pa-1"></v-divider>
<div class="d-flex justify-center"> <div class="d-flex justify-center">
<v-btn color="orange-darken-4" size="small" @click="addQuestion">Добавить вопрос</v-btn>
<v-btn size="small" color="orange-darken-4" @click="addQuestion">Добавить вопрос</v-btn>
</div> </div>
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
@@ -267,17 +253,25 @@
</v-stepper-window-item> </v-stepper-window-item>
<!-- Подтверждение --> <!-- Подтверждение -->
<v-stepper-window-item step="3" class="fill-height"> <v-stepper-window-item class="fill-height" step="3">
<v-card flat class="fill-height d-flex flex-column"> <v-card class="fill-height d-flex flex-column" flat>
<v-card-title>Подтверждение</v-card-title> <v-card-title>Подтверждение</v-card-title>
<v-card-text style="max-height: 62vh; overflow-y: auto;"> <v-card-text style="max-height: 62vh; overflow-y: auto;">
<v-alert v-if="errorMessage" class="mb-4 " type="error"> <v-alert v-if="errorMessage" class="mb-4 " type="error">
{{ errorMessage }} {{ errorMessage }}
</v-alert> </v-alert>
<p><v-icon size="xsmall" :class="form.title===null||form.title.length==0?'':'opacity-0'" color='red' icon="mdi-alert"/><strong class="px-2">Название:</strong> {{ form.title }}</p> <p>
<p><v-icon size="xsmall" :class="form.description===null||form.description.length==0?'':'opacity-0'" color='red' icon="mdi-alert"/><strong class="px-2">Описание:</strong> {{ form.description }}</p> <v-icon :class="form.title===null||form.title.length===0?'':'opacity-0'" color='red' icon="mdi-alert"
size="xsmall"/>
<strong class="px-2">Название:</strong> {{ form.title }}
</p>
<p>
<v-icon :class="form.description===null||form.description.length===0?'':'opacity-0'" color='red'
icon="mdi-alert" size="xsmall"/>
<strong class="px-2">Описание:</strong> {{ form.description }}
</p>
<p><strong class="px-2">Дата начала:</strong> {{ form.startDate.toLocaleDateString() }}</p> <p><strong class="px-2">Дата начала:</strong> {{ form.startDate.toLocaleDateString() }}</p>
<p><strong class="px-2">Дата окончания:</strong> {{ form.endDate.toLocaleDateString() }}</p> <p><strong class="px-2">Дата окончания:</strong> {{ form.endDate.toLocaleDateString() }}</p>
<p><strong class="px-2">Вопросы:</strong></p> <p><strong class="px-2">Вопросы:</strong></p>
@@ -299,7 +293,9 @@
</div> </div>
<div v-if="question.settings.choice === 'employee' && question.answers.length"> <div v-if="question.settings.choice === 'employee' && question.answers.length">
<div>Количество допустимых ответов: {{ question.settings.multiChoice }}</div> <div>Количество допустимых ответов: {{ question.settings.multiChoice }}</div>
<div class="">Голосование по отделам: {{ onlyDepdict.find(e=>e.value===question.settings.onlyDep).name}}</div> <div class="">Голосование по отделам:
{{ onlyDepdict.find(e => e.value === question.settings.onlyDep).name }}
</div>
<p><strong>Выбранные сотрудники:</strong></p> <p><strong>Выбранные сотрудники:</strong></p>
<ul> <ul>
<li v-for="employeeId in question.answers" :key="employeeId"> <li v-for="employeeId in question.answers" :key="employeeId">
@@ -313,7 +309,9 @@
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
<v-btn color="teal-darken-4" size="medium" @click="step -= 1">Назад</v-btn> <v-btn color="teal-darken-4" size="medium" @click="step -= 1">Назад</v-btn>
<v-btn :disabled="errorMessage!=''" color="teal-darken-4" size="medium" @click="saveVoting">Сохранить</v-btn> <v-btn :disabled="errorMessage!=''" color="teal-darken-4" size="medium" @click="saveVoting">
Сохранить
</v-btn>
</v-card-actions> </v-card-actions>
</v-card> </v-card>
@@ -330,17 +328,14 @@
<script> <script>
import {computed, ref, watch} from "vue"; import {computed, ref, watch} from "vue";
import { spravURL } from "@/utils/axios-api"; import {createVote, getEmployees, spravURL} from "@/utils/axios-api";
import { createVote, getEmployees } from "@/utils/axios-api";
import DateSelect from "@/components/UI/date-select.vue"; import DateSelect from "@/components/UI/date-select.vue";
import { useAccountStore } from "@/store/AccountStore";
import {checkremote} from "@/utils/universal_functions"; import {checkremote} from "@/utils/universal_functions";
export default { export default {
components: {DateSelect}, components: {DateSelect},
emits: ['updating'], emits: ['updating'],
setup(props, {emit}) { setup(props, {emit}) {
const accountStore = useAccountStore();
const user = accountStore.user;
const step = ref(0); const step = ref(0);
const isVoteCreated = ref(false); const isVoteCreated = ref(false);
const form = ref({ const form = ref({
@@ -382,7 +377,6 @@ export default {
const handleSelectAll = (selected, index) => { const handleSelectAll = (selected, index) => {
console.log(selected, index);
if (!selected) { if (!selected) {
form.value.questions[index].answers = groupedEmployees.value.map((employee) => ({ form.value.questions[index].answers = groupedEmployees.value.map((employee) => ({
text: employee.id, text: employee.id,
@@ -415,6 +409,10 @@ export default {
} }
}; };
const updateQuest = (index) => {
quests[index] = form.value.questions[index].text.length > 0;
};
const addAnswer = (index) => { const addAnswer = (index) => {
if (currentQuestion.value) { if (currentQuestion.value) {
form.value.questions[index].answers.push({text: "", image: null}); form.value.questions[index].answers.push({text: "", image: null});
@@ -422,16 +420,14 @@ export default {
}; };
const removeAnswer = (index, ind) => { const removeAnswer = (index, ind) => {
// if (currentQuestion.value) {
form.value.questions[index].answers.splice(ind, 1) form.value.questions[index].answers.splice(ind, 1)
// currentQuestion.value.answers.splice(index, 1);
// }
}; };
const errorMessage = ref(""); const errorMessage = ref("");
const saveVoting = async () => { const saveVoting = async () => {
if (!form.value.title || !form.value.description || form.value.questions.length < 1) { const hasEmptyQuestions = quests.value.includes(false);
if (!form.value.title || !form.value.description || form.value.questions.length < 1 || hasEmptyQuestions) {
errorMessage.value = "Все обязательные поля должны быть заполнены"; errorMessage.value = "Все обязательные поля должны быть заполнены";
return; return;
} }
@@ -439,12 +435,13 @@ export default {
const response = await createVote(form.value); const response = await createVote(form.value);
console.log("Голосование успешно создано:", response); console.log("Голосование успешно создано:", response);
isVoteCreated.value = true; isVoteCreated.value = true;
emit('updating', 'active') emit('updating', 'active');
} catch (error) { } catch (error) {
console.error("Ошибка при создании голосования:", error); console.error("Ошибка при создании голосования:", error);
} }
}; };
const resetForm = () => { const resetForm = () => {
form.value = { form.value = {
title: "", title: "",
@@ -472,12 +469,27 @@ export default {
); );
watch(form.value, () => { watch(form.value, () => {
if (!form.value.title || !form.value.description || form.value.questions < 1) { const hasEmptyQuestions = quests.value.includes(false);
if (!form.value.title || !form.value.description || form.value.questions.length < 1 || hasEmptyQuestions) {
errorMessage.value = "Все обязательные поля должны быть заполнены"; errorMessage.value = "Все обязательные поля должны быть заполнены";
} else { } else {
errorMessage.value = ""; errorMessage.value = "";
} }
}) }, { deep: true });
const updateQuests = () => {
quests.value = form.value.questions.map((q) => !!q.text.trim());
};
watch(() => form.value.questions, updateQuests, { deep: true });
watch(() => form.value.questions, (newQuestions) => {
const hasEmptyQuestions = newQuestions.some((q) => !q.text || q.text.trim() === "");
errorMessage.value = hasEmptyQuestions
? "Все вопросы должны быть заполнены"
: "";
}, { deep: true });
watch(selectedTimeStart, () => { watch(selectedTimeStart, () => {
updateStartDate() updateStartDate()
@@ -503,23 +515,30 @@ export default {
form.value.endDate = dates form.value.endDate = dates
} }
const selectedEmployees = ref([]);
const onEmployeesChange = (selected, index) => { const onEmployeesChange = (selected, index) => {
let ind = form.value.questions[index].selectedEmployees.findIndex(e=>e==selected) let ind = form.value.questions[index].selectedEmployees.findIndex(e => e === selected)
if (ind > -1) { if (ind > -1) {
form.value.questions[index].selectedEmployees.splice(ind, 1) form.value.questions[index].selectedEmployees.splice(ind, 1)
let answ =form.value.questions[index].answers.findIndex(a=>a.text==selected) let answ = form.value.questions[index].answers.findIndex(a => a.text === selected)
form.value.questions[index].answers.splice(answ, 1) form.value.questions[index].answers.splice(answ, 1)
} } else {
else{
form.value.questions[index].selectedEmployees.push(selected) form.value.questions[index].selectedEmployees.push(selected)
form.value.questions[index].answers.push({text: selected, image: null}); form.value.questions[index].answers.push({text: selected, image: null});
} }
}; };
const onlyDepdict = [{name:'За любого сотрудника' ,value:null},{name:"Кроме своего отдела" ,value:false}, {name:"Только за свой отдел" ,value:true}] const onlyDepdict = [{name: 'За любого сотрудника', value: null}, {
name: "Кроме своего отдела",
value: false
}, {name: "Только за свой отдел", value: true}]
const quests = computed(() =>
form.value.questions.map((question) => question.text.length > 0)
);
return { return {
quests,
updateQuest,
onlyDepdict, onlyDepdict,
allEmployeesgroupedbydep, allEmployeesgroupedbydep,
onEmployeesChange, onEmployeesChange,
@@ -555,10 +574,12 @@ export default {
.v-input--horizontal .v-input__prepend { .v-input--horizontal .v-input__prepend {
margin-inline-end: 4px; margin-inline-end: 4px;
} }
.list-enter-active, .list-enter-active,
.list-leave-active { .list-leave-active {
transition: all 0.5s ease; transition: all 0.5s ease;
} }
.list-enter-from, .list-enter-from,
.list-leave-to { .list-leave-to {
opacity: 0; opacity: 0;