From 78abcce75802060bbe7fc5f7c5523e7a8e80df0c Mon Sep 17 00:00:00 2001 From: ricola Date: Thu, 4 Dec 2025 01:47:17 +0100 Subject: [PATCH] Validate vote title --- vedia.rb | 38 +++++++++++++++++++++++--------- views/votes_edit_description.erb | 8 +++++++ views/votes_new.erb | 8 +++++++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/vedia.rb b/vedia.rb index 63d1142..1bd41b4 100644 --- a/vedia.rb +++ b/vedia.rb @@ -277,12 +277,21 @@ end post '/votes/new' do require_login - @vote = Vote.create(secure_id: SecureRandom.hex(8), - title: params[:title], - description: params[:description], - state: 'draft') - @vote.users << current_user - redirect '/votes/' + @vote.secure_id + @errors = [] + if params[:title].empty? + @errors << OpenStruct.new(:attribute => :title, :type => :blank) + end + if not @errors.empty? + @params = params + erb :votes_new + else + @vote = Vote.create(secure_id: SecureRandom.hex(8), + title: params[:title], + description: params[:description], + state: 'draft') + @vote.users << current_user + redirect '/votes/' + @vote.secure_id + end end get '/votes/:id' do @@ -315,10 +324,19 @@ post '/votes/:id/edit' do find_vote require_organizer require_draft_vote - @vote.title = params[:title] - @vote.description = params[:description] - @vote.save - redirect '/votes/' + @vote.secure_id + @errors = [] + if params[:title].empty? + @errors << OpenStruct.new(:attribute => :title, :type => :blank) + end + if not @errors.empty? + @params = params + erb :votes_edit_description + else + @vote.title = params[:title] + @vote.description = params[:description] + @vote.save + redirect '/votes/' + @vote.secure_id + end end post '/votes/:id/candidates' do diff --git a/views/votes_edit_description.erb b/views/votes_edit_description.erb index a4af2a5..d419702 100644 --- a/views/votes_edit_description.erb +++ b/views/votes_edit_description.erb @@ -1,5 +1,13 @@

<%= _("Edit vote description") %>

+<% if @errors %> +<% @errors.each do |error| %> + <% if error.attribute == :title and error.type == :blank %> +

<%= _("Enter a title.") %>

+ <% end %> +<% end %> +<% end %> +
diff --git a/views/votes_new.erb b/views/votes_new.erb index 2ef0ddf..ed0c93c 100644 --- a/views/votes_new.erb +++ b/views/votes_new.erb @@ -1,5 +1,13 @@

<%= _("New vote") %>

+<% if @errors %> +<% @errors.each do |error| %> + <% if error.attribute == :title and error.type == :blank %> +

<%= _("Enter a title.") %>

+ <% end %> +<% end %> +<% end %> +