Validate vote title

This commit is contained in:
ricola 2025-12-04 01:47:17 +01:00
parent 4ad1c4cb86
commit 78abcce758
3 changed files with 44 additions and 10 deletions

View file

@ -277,12 +277,21 @@ end
post '/votes/new' do post '/votes/new' do
require_login require_login
@vote = Vote.create(secure_id: SecureRandom.hex(8), @errors = []
title: params[:title], if params[:title].empty?
description: params[:description], @errors << OpenStruct.new(:attribute => :title, :type => :blank)
state: 'draft') end
@vote.users << current_user if not @errors.empty?
redirect '/votes/' + @vote.secure_id @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 end
get '/votes/:id' do get '/votes/:id' do
@ -315,10 +324,19 @@ post '/votes/:id/edit' do
find_vote find_vote
require_organizer require_organizer
require_draft_vote require_draft_vote
@vote.title = params[:title] @errors = []
@vote.description = params[:description] if params[:title].empty?
@vote.save @errors << OpenStruct.new(:attribute => :title, :type => :blank)
redirect '/votes/' + @vote.secure_id 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 end
post '/votes/:id/candidates' do post '/votes/:id/candidates' do

View file

@ -1,5 +1,13 @@
<h1 class="mb-5"><%= _("Edit vote description") %></h1> <h1 class="mb-5"><%= _("Edit vote description") %></h1>
<% if @errors %>
<% @errors.each do |error| %>
<% if error.attribute == :title and error.type == :blank %>
<p class="alert alert-warning mb-4"><%= _("Enter a title.") %></p>
<% end %>
<% end %>
<% end %>
<form action="/votes/<%= @vote.secure_id %>/edit" method="post"> <form action="/votes/<%= @vote.secure_id %>/edit" method="post">
<div class="mb-3"> <div class="mb-3">
<label for="title" class="form-label"><%= _("Title") %></label> <label for="title" class="form-label"><%= _("Title") %></label>

View file

@ -1,5 +1,13 @@
<h1 class="mb-5"><%= _("New vote") %></h1> <h1 class="mb-5"><%= _("New vote") %></h1>
<% if @errors %>
<% @errors.each do |error| %>
<% if error.attribute == :title and error.type == :blank %>
<p class="alert alert-warning mb-4"><%= _("Enter a title.") %></p>
<% end %>
<% end %>
<% end %>
<form action="/votes/new" method="post"> <form action="/votes/new" method="post">
<div class="mb-3"> <div class="mb-3">
<label for="title" class="form-label"><%= _("Title") %></label> <label for="title" class="form-label"><%= _("Title") %></label>