From f24373995458535d0c8e46e83da0e8e27c410871 Mon Sep 17 00:00:00 2001 From: shpuld Date: Tue, 18 Jun 2019 17:42:31 +0300 Subject: [PATCH] Add basic clientside validation for polls --- src/components/poll/poll_form.js | 8 +++++++- src/components/post_status_form/post_status_form.js | 9 +++++++++ src/i18n/en.json | 3 ++- src/i18n/fi.json | 3 ++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/components/poll/poll_form.js b/src/components/poll/poll_form.js index 3b47c454..c0c1ccf7 100644 --- a/src/components/poll/poll_form.js +++ b/src/components/poll/poll_form.js @@ -1,4 +1,5 @@ import * as DateUtils from 'src/services/date_utils/date_utils.js' +import { uniq } from 'lodash' export default { name: 'PollForm', @@ -105,8 +106,13 @@ export default { this.expiryAmount ) + const options = uniq(this.options.filter(option => option !== '')) + if (options.length < 2) { + this.$emit('update-poll', { error: this.$t('polls.not_enough_options') }) + return + } this.$emit('update-poll', { - options: this.options, + options, multiple: this.pollType === 'multiple', expiresIn }) diff --git a/src/components/post_status_form/post_status_form.js b/src/components/post_status_form/post_status_form.js index ae4050c9..454a05ee 100644 --- a/src/components/post_status_form/post_status_form.js +++ b/src/components/post_status_form/post_status_form.js @@ -193,6 +193,11 @@ const PostStatusForm = { }, hideScopeNotice () { return this.$store.state.config.hideScopeNotice + }, + pollContentError () { + return this.pollFormVisible && + this.newStatus.poll && + this.newStatus.poll.error } }, methods: { @@ -261,6 +266,10 @@ const PostStatusForm = { } const poll = this.pollFormVisible ? this.newStatus.poll : {} + if (this.pollContentError) { + this.error = this.pollContentError + return + } this.posting = true statusPoster.postStatus({ diff --git a/src/i18n/en.json b/src/i18n/en.json index 3909135c..73f421c8 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -102,7 +102,8 @@ "multiple_choices": "Multiple choices", "expiry": "Poll age", "expires_in": "Ends in {0}", - "expired": "Poll ended {0} ago" + "expired": "Poll ended {0} ago", + "not_enough_options": "Too few unique options in poll" }, "interactions": { "favs_repeats": "Repeats and Favorites", diff --git a/src/i18n/fi.json b/src/i18n/fi.json index 9c8cd863..f4179495 100644 --- a/src/i18n/fi.json +++ b/src/i18n/fi.json @@ -66,7 +66,8 @@ "multiple_choices": "Monivalinta", "expiry": "Äänestyksen kesto", "expires_in": "Päättyy {0} päästä", - "expired": "Päättyi {0} sitten" + "expired": "Päättyi {0} sitten", + "not_enough_option": "Liian vähän uniikkeja vaihtoehtoja äänestyksessä" }, "interactions": { "favs_repeats": "Toistot ja tykkäykset",