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,6 +277,14 @@ end
post '/votes/new' do
require_login
@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],
@ -284,6 +292,7 @@ post '/votes/new' do
@vote.users << current_user
redirect '/votes/' + @vote.secure_id
end
end
get '/votes/:id' do
require_login
@ -315,11 +324,20 @@ post '/votes/:id/edit' do
find_vote
require_organizer
require_draft_vote
@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
require_login

View file

@ -1,5 +1,13 @@
<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">
<div class="mb-3">
<label for="title" class="form-label"><%= _("Title") %></label>

View file

@ -1,5 +1,13 @@
<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">
<div class="mb-3">
<label for="title" class="form-label"><%= _("Title") %></label>