FIX: Add a blank poll options validation (#8652)

Before, saving a post with a blank poll option resulted in error 500.
This commit is contained in:
Jarek Radosz
2020-01-03 15:09:36 +01:00
committed by GitHub
parent fac71da605
commit 9e3fc1111d
3 changed files with 48 additions and 0 deletions

View File

@ -17,6 +17,7 @@ module DiscoursePoll
return false unless valid_numbers?(poll)
return false unless unique_poll_name?(polls, poll)
return false unless unique_options?(poll)
return false unless any_blank_options?(poll)
return false unless at_least_two_options?(poll)
return false unless valid_number_of_options?(poll)
return false unless valid_multiple_choice_settings?(poll)
@ -77,6 +78,20 @@ module DiscoursePoll
true
end
def any_blank_options?(poll)
if poll["options"].any? { |o| o["html"].blank? }
if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME
@post.errors.add(:base, I18n.t("poll.default_poll_must_not_have_any_empty_options"))
else
@post.errors.add(:base, I18n.t("poll.named_poll_must_not_have_any_empty_options", name: poll["name"]))
end
return false
end
true
end
def at_least_two_options?(poll)
if poll["options"].size < 2
if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME