Validate candidate name
This commit is contained in:
parent
f8b5f80823
commit
b2e76f4363
3 changed files with 49 additions and 14 deletions
40
vedia.rb
40
vedia.rb
|
|
@ -346,11 +346,20 @@ post '/votes/:id/candidates' do
|
||||||
find_vote
|
find_vote
|
||||||
require_organizer
|
require_organizer
|
||||||
require_draft_vote
|
require_draft_vote
|
||||||
@candidate = Candidate.new(name: params[:name],
|
@errors = []
|
||||||
description: params[:description])
|
if params[:name].empty?
|
||||||
@candidate.vote = @vote
|
@errors << OpenStruct.new(:attribute => :name, :type => :blank)
|
||||||
@candidate.save
|
end
|
||||||
redirect '/votes/' + @vote.secure_id
|
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
|
end
|
||||||
|
|
||||||
get '/votes/:id/candidates/:cid' do
|
get '/votes/:id/candidates/:cid' do
|
||||||
|
|
@ -360,6 +369,8 @@ get '/votes/:id/candidates/:cid' do
|
||||||
require_draft_vote
|
require_draft_vote
|
||||||
find_candidate
|
find_candidate
|
||||||
require_candidate_in_vote
|
require_candidate_in_vote
|
||||||
|
params[:name] = @candidate.name
|
||||||
|
params[:description] = @candidate.description
|
||||||
erb :candidates_edit
|
erb :candidates_edit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -370,11 +381,20 @@ post '/votes/:id/candidates/:cid' do
|
||||||
require_draft_vote
|
require_draft_vote
|
||||||
find_candidate
|
find_candidate
|
||||||
require_candidate_in_vote
|
require_candidate_in_vote
|
||||||
@candidate.name = params[:name]
|
@errors = []
|
||||||
@candidate.description = params[:description]
|
if params[:name].empty?
|
||||||
@candidate.save
|
@errors << OpenStruct.new(:attribute => :name, :type => :blank)
|
||||||
redirect '/votes/' + @vote.secure_id
|
end
|
||||||
erb :candidates_edit
|
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
|
end
|
||||||
|
|
||||||
post '/votes/:id/candidates/:cid/delete' do
|
post '/votes/:id/candidates/:cid/delete' do
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,21 @@
|
||||||
<h1 class="mb-5"><%= _("Edit candidate") %></h1>
|
<h1 class="mb-5"><%= _("Edit candidate") %></h1>
|
||||||
|
|
||||||
|
<% if @errors %>
|
||||||
|
<% @errors.each do |error| %>
|
||||||
|
<% if error.attribute == :name and error.type == :blank %>
|
||||||
|
<p class="alert alert-warning mb-4"><%= _("Enter a name.") %></p>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<form action="/votes/<%= @vote.secure_id %>/candidates/<%= @candidate.id %>" method="post">
|
<form action="/votes/<%= @vote.secure_id %>/candidates/<%= @candidate.id %>" method="post">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="name" class="form-label"><%= _("Name") %></label>
|
<label for="name" class="form-label"><%= _("Name") %></label>
|
||||||
<input type="text" name="name" value="<%= @candidate.name %>" class="form-control">
|
<input type="text" name="name" value="<%= params[:name] %>" class="form-control">
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="description" class="form-label"><%= _("Description") %></label>
|
<label for="description" class="form-label"><%= _("Description") %></label>
|
||||||
<textarea type="text" name="description" class="form-control"><%= @candidate.description %></textarea>
|
<textarea type="text" name="description" class="form-control"><%= params[:description] %></textarea>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-primary"><%= _("Save") %></button>
|
<button type="submit" class="btn btn-primary"><%= _("Save") %></button>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
|
|
@ -40,14 +40,21 @@
|
||||||
<% if @vote.candidates.empty? %>
|
<% if @vote.candidates.empty? %>
|
||||||
<p class="alert alert-primary mb-4"><%= _("This vote has no candidates yet. Add a first candidate.") %></p>
|
<p class="alert alert-primary mb-4"><%= _("This vote has no candidates yet. Add a first candidate.") %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
<% if @errors %>
|
||||||
|
<% @errors.each do |error| %>
|
||||||
|
<% if error.attribute == :name and error.type == :blank %>
|
||||||
|
<p class="alert alert-warning mb-4"><%= _("Enter a name.") %></p>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="name" class="form-label"><%= _("Name") %></label>
|
<label for="name" class="form-label"><%= _("Name") %></label>
|
||||||
<input type="text" name="name" class="form-control">
|
<input type="text" name="name" value="<%= params[:name] %>" class="form-control">
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="description" class="form-label"><%= _("Description") %></label>
|
<label for="description" class="form-label"><%= _("Description") %></label>
|
||||||
<textarea type="text" name="description" class="form-control"></textarea>
|
<textarea type="text" name="description" class="form-control"><%= params[:description] %></textarea>
|
||||||
</div>
|
</div>
|
||||||
<% if @vote.candidates.length < 2 %>
|
<% if @vote.candidates.length < 2 %>
|
||||||
<button type="submit" class="btn btn-primary"><%= _("Add candidate") %></button>
|
<button type="submit" class="btn btn-primary"><%= _("Add candidate") %></button>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue