From b2e76f43631a700db195b55865b5c7b82369116c Mon Sep 17 00:00:00 2001 From: ricola Date: Thu, 4 Dec 2025 02:23:04 +0100 Subject: [PATCH] Validate candidate name --- vedia.rb | 40 +++++++++++++++++++++++++++++---------- views/candidates_edit.erb | 12 ++++++++++-- views/votes_edit.erb | 11 +++++++++-- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/vedia.rb b/vedia.rb index 448bcb9..a3ef682 100644 --- a/vedia.rb +++ b/vedia.rb @@ -346,11 +346,20 @@ post '/votes/:id/candidates' do find_vote require_organizer require_draft_vote - @candidate = Candidate.new(name: params[:name], - description: params[:description]) - @candidate.vote = @vote - @candidate.save - redirect '/votes/' + @vote.secure_id + @errors = [] + if params[:name].empty? + @errors << OpenStruct.new(:attribute => :name, :type => :blank) + end + if not @errors.empty? + @params = params + erb :votes_edit + else + @candidate = Candidate.new(name: params[:name], + description: params[:description]) + @candidate.vote = @vote + @candidate.save + redirect '/votes/' + @vote.secure_id + end end get '/votes/:id/candidates/:cid' do @@ -360,6 +369,8 @@ get '/votes/:id/candidates/:cid' do require_draft_vote find_candidate require_candidate_in_vote + params[:name] = @candidate.name + params[:description] = @candidate.description erb :candidates_edit end @@ -370,11 +381,20 @@ post '/votes/:id/candidates/:cid' do require_draft_vote find_candidate require_candidate_in_vote - @candidate.name = params[:name] - @candidate.description = params[:description] - @candidate.save - redirect '/votes/' + @vote.secure_id - erb :candidates_edit + @errors = [] + if params[:name].empty? + @errors << OpenStruct.new(:attribute => :name, :type => :blank) + end + if not @errors.empty? + @params = params + erb :candidates_edit + else + @candidate.name = params[:name] + @candidate.description = params[:description] + @candidate.save + redirect '/votes/' + @vote.secure_id + erb :candidates_edit + end end post '/votes/:id/candidates/:cid/delete' do diff --git a/views/candidates_edit.erb b/views/candidates_edit.erb index 16b62d7..0780d85 100644 --- a/views/candidates_edit.erb +++ b/views/candidates_edit.erb @@ -1,13 +1,21 @@

<%= _("Edit candidate") %>

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

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

+ <% end %> +<% end %> +<% end %> +
- +
- +
diff --git a/views/votes_edit.erb b/views/votes_edit.erb index c631804..6b55b91 100644 --- a/views/votes_edit.erb +++ b/views/votes_edit.erb @@ -40,14 +40,21 @@ <% if @vote.candidates.empty? %>

<%= _("This vote has no candidates yet. Add a first candidate.") %>

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

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

+ <% end %> + <% end %> + <% end %>
- +
- +
<% if @vote.candidates.length < 2 %>