Валидация ответов

This commit is contained in:
Denzel committed 2025-01-24 13:53:26 +03:00
1 parent f4573f0d43
commit b58786085d
1 file changed
+61 -18
@@ -142,13 +142,29 @@
<div v-if="question.settings.choice === 'free'">
<transition-group name="list" tag="div">
<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]"
class="mr-4" density="compact" variant="outlined"></v-text-field>
<v-file-input v-model="answer.image" accept="image/*" dense density="compact" hide-details
label="Картинка" variant="outlined"></v-file-input>
<v-btn class="ms-1" color="error" icon="mdi-delete" size="small"
@click="removeAnswer(index,ind)">
<v-text-field
v-model="answer.text"
:label="'Ответ ' + (ind + 1)"
:rules="[rules.required]"
class="mr-4"
density="compact"
variant="outlined">
</v-text-field>
<v-file-input
v-model="answer.image"
accept="image/*"
dense
density="compact"
hide-details
label="Картинка"
variant="outlined">
</v-file-input>
<v-btn
class="ms-1"
color="error"
icon="mdi-delete"
size="small"
@click="removeAnswer(index, ind)">
</v-btn>
</div>
</transition-group>
@@ -425,17 +441,37 @@ export default {
const errorMessage = ref("");
const validateAnswers = () => {
return form.value.questions.some((question) => {
if (question.settings.choice === "free") {
return question.answers.some(
(answer) => !answer.text || answer.text.trim() === ""
);
}
return false;
});
};
const saveVoting = async () => {
const hasEmptyQuestions = quests.value.includes(false);
if (!form.value.title || !form.value.description || form.value.questions.length < 1 || hasEmptyQuestions) {
errorMessage.value = "Все обязательные поля должны быть заполнены";
const hasInvalidAnswers = validateAnswers();
if (
!form.value.title ||
!form.value.description ||
form.value.questions.length < 1 ||
hasEmptyQuestions ||
hasInvalidAnswers
) {
errorMessage.value = "Все обязательные поля и ответы должны быть заполнены";
return;
}
try {
const response = await createVote(form.value);
console.log("Голосование успешно создано:", response);
isVoteCreated.value = true;
emit('updating', 'active');
emit("updating", "active");
} catch (error) {
console.error("Ошибка при создании голосования:", error);
}
@@ -469,8 +505,7 @@ export default {
);
watch(form.value, () => {
const hasEmptyQuestions = quests.value.includes(false);
if (!form.value.title || !form.value.description || form.value.questions.length < 1 || hasEmptyQuestions) {
if (!form.value.title || !form.value.description || form.value.questions.length < 1) {
errorMessage.value = "Все обязательные поля должны быть заполнены";
} else {
errorMessage.value = "";
@@ -483,12 +518,20 @@ export default {
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(
() => form.value.questions,
(newQuestions) => {
const hasEmptyQuestions = newQuestions.some((q) => !q.text || q.text.trim() === "");
const hasInvalidAnswers = validateAnswers();
if (hasEmptyQuestions || hasInvalidAnswers) {
errorMessage.value = "Все вопросы и ответы должны быть заполнены";
} else {
errorMessage.value = "";
}
},
{ deep: true }
);
watch(selectedTimeStart, () => {