Skip to content

Commit

Permalink
Added server-side validation for game setup form
Browse files Browse the repository at this point in the history
  • Loading branch information
lisahamm committed Feb 24, 2015
1 parent 3b81ac5 commit c13e298
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ input#setup-submit {
width: 80px;
}

#setup-form p {
color: red;
font-size: 0.9em;
font-weight: 300;
margin: 5px;
}

input#cell-btn {
border: 1px solid black;
border-radius: 3px;
Expand Down
5 changes: 5 additions & 0 deletions tic_tac_toe_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ class TicTacToeController < Sinatra::Base
end

post '/setup' do
redirect to('/invalid_setup') if params[:player_mark] == nil || params[:opponent] == nil
session[:mark] = params[:player_mark]
session[:opponent] = params[:opponent]
redirect to('/game')
end

get '/invalid_setup' do
erb :index_with_validation_messages
end

get '/game' do
@board = TicTacToe::Board.new(cells: session[:moves])
session[:moves] = @board.to_array
Expand Down
29 changes: 29 additions & 0 deletions views/index_with_validation_messages.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div>
<form id="setup-form" action="/setup" method="POST">

<div class="form-section">
Player 1, select your mark:
<input type="radio" name="player_mark" value="X" />X
<input type="radio" name="player_mark" value="O" />O
</div>

<% if params[:player_mark] == nil %>
<p>*Please select a mark in order to continue.</p>
<% end %>

<div class="form-section">
Would you like to play against the computer?:
<input type="radio" name="opponent" value="yes" />Yes
<input type="radio" name="opponent" value="no" />No
</div>

<% if params[:opponent] == nil %>
<p>*Please indicate if you would like to play against the computer in order to continue.</p>
<% end %>

<div class="form-section">
<input id="setup-submit" type ="submit" value="Submit" />
</div>
</form>
</div>

0 comments on commit c13e298

Please sign in to comment.