Валидация ответов
This commit is contained in:
1 parent
f4573f0d43
commit
b58786085d
1 file changed
+59
-16
@@ -142,13 +142,29 @@
|
|||||||
<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)"
|
<v-text-field
|
||||||
|
v-model="answer.text"
|
||||||
|
:label="'Ответ ' + (ind + 1)"
|
||||||
:rules="[rules.required]"
|
:rules="[rules.required]"
|
||||||
class="mr-4" density="compact" variant="outlined"></v-text-field>
|
class="mr-4"
|
||||||
<v-file-input v-model="answer.image" accept="image/*" dense density="compact" hide-details
|
density="compact"
|
||||||
label="Картинка" variant="outlined"></v-file-input>
|
variant="outlined">
|
||||||
<v-btn class="ms-1" color="error" icon="mdi-delete" size="small"
|
</v-text-field>
|
||||||
@click="removeAnswer(index,ind)">
|
<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>
|
</v-btn>
|
||||||
</div>
|
</div>
|
||||||
</transition-group>
|
</transition-group>
|
||||||
@@ -425,17 +441,37 @@ export default {
|
|||||||
|
|
||||||
const errorMessage = ref("");
|
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 saveVoting = async () => {
|
||||||
const hasEmptyQuestions = quests.value.includes(false);
|
const hasEmptyQuestions = quests.value.includes(false);
|
||||||
if (!form.value.title || !form.value.description || form.value.questions.length < 1 || hasEmptyQuestions) {
|
const hasInvalidAnswers = validateAnswers();
|
||||||
errorMessage.value = "Все обязательные поля должны быть заполнены";
|
|
||||||
|
if (
|
||||||
|
!form.value.title ||
|
||||||
|
!form.value.description ||
|
||||||
|
form.value.questions.length < 1 ||
|
||||||
|
hasEmptyQuestions ||
|
||||||
|
hasInvalidAnswers
|
||||||
|
) {
|
||||||
|
errorMessage.value = "Все обязательные поля и ответы должны быть заполнены";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -469,8 +505,7 @@ export default {
|
|||||||
);
|
);
|
||||||
|
|
||||||
watch(form.value, () => {
|
watch(form.value, () => {
|
||||||
const hasEmptyQuestions = quests.value.includes(false);
|
if (!form.value.title || !form.value.description || form.value.questions.length < 1) {
|
||||||
if (!form.value.title || !form.value.description || form.value.questions.length < 1 || hasEmptyQuestions) {
|
|
||||||
errorMessage.value = "Все обязательные поля должны быть заполнены";
|
errorMessage.value = "Все обязательные поля должны быть заполнены";
|
||||||
} else {
|
} else {
|
||||||
errorMessage.value = "";
|
errorMessage.value = "";
|
||||||
@@ -483,12 +518,20 @@ export default {
|
|||||||
|
|
||||||
watch(() => form.value.questions, updateQuests, { deep: true });
|
watch(() => form.value.questions, updateQuests, { deep: true });
|
||||||
|
|
||||||
watch(() => form.value.questions, (newQuestions) => {
|
watch(
|
||||||
|
() => form.value.questions,
|
||||||
|
(newQuestions) => {
|
||||||
const hasEmptyQuestions = newQuestions.some((q) => !q.text || q.text.trim() === "");
|
const hasEmptyQuestions = newQuestions.some((q) => !q.text || q.text.trim() === "");
|
||||||
errorMessage.value = hasEmptyQuestions
|
const hasInvalidAnswers = validateAnswers();
|
||||||
? "Все вопросы должны быть заполнены"
|
|
||||||
: "";
|
if (hasEmptyQuestions || hasInvalidAnswers) {
|
||||||
}, { deep: true });
|
errorMessage.value = "Все вопросы и ответы должны быть заполнены";
|
||||||
|
} else {
|
||||||
|
errorMessage.value = "";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ deep: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
watch(selectedTimeStart, () => {
|
watch(selectedTimeStart, () => {
|
||||||
|
|||||||
Reference in new issue
Block a user