adds validation for price fields
This commit is contained in:
@@ -17,8 +17,11 @@ class Valuation::SpendingProposalsController < Valuation::BaseController
|
||||
end
|
||||
|
||||
def valuate
|
||||
@spending_proposal.update_attributes(valuation_params)
|
||||
if valid_price_params? && @spending_proposal.update(valuation_params)
|
||||
redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate')
|
||||
else
|
||||
render action: :edit
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
@@ -35,4 +38,16 @@ class Valuation::SpendingProposalsController < Valuation::BaseController
|
||||
raise ActionController::RoutingError.new('Not Found') unless current_user.administrator? || ValuationAssignment.exists?(spending_proposal_id: params[:id], valuator_id: current_user.valuator.id)
|
||||
end
|
||||
|
||||
def valid_price_params?
|
||||
if /\D/.match params[:spending_proposal][:price]
|
||||
@spending_proposal.errors.add(:price, I18n.t('spending_proposals.wrong_price_format'))
|
||||
end
|
||||
|
||||
if /\D/.match params[:spending_proposal][:price_first_year]
|
||||
@spending_proposal.errors.add(:price_first_year, I18n.t('spending_proposals.wrong_price_format'))
|
||||
end
|
||||
|
||||
@spending_proposal.errors.empty?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -423,6 +423,7 @@ en:
|
||||
recommendation_two: Any proposal or comment suggesting illegal action will be deleted.
|
||||
recommendations_title: How to create a spending proposal
|
||||
start_new: Create spending proposal
|
||||
wrong_price_format: Only integer numbers
|
||||
stats:
|
||||
index:
|
||||
visits: Visits
|
||||
|
||||
@@ -423,6 +423,7 @@ es:
|
||||
recommendation_two: Cualquier propuesta o comentario que implique acciones ilegales será eliminada.
|
||||
recommendations_title: Cómo crear una propuesta de gasto
|
||||
start_new: Crear una propuesta de gasto
|
||||
wrong_price_format: Solo puede incluir caracteres numéricos
|
||||
stats:
|
||||
index:
|
||||
visits: Visitas
|
||||
|
||||
@@ -284,6 +284,20 @@ feature 'Valuation spending proposals' do
|
||||
click_link @spending_proposal.title
|
||||
expect(page).to have_content('Valuation finished')
|
||||
end
|
||||
|
||||
scenario 'Validates price formats' do
|
||||
visit valuation_spending_proposals_path
|
||||
within("#spending_proposal_#{@spending_proposal.id}") do
|
||||
click_link "Edit"
|
||||
end
|
||||
|
||||
fill_in 'spending_proposal_price', with: '12345,98'
|
||||
fill_in 'spending_proposal_price_first_year', with: '9876.6'
|
||||
click_button 'Save changes'
|
||||
|
||||
expect(page).to have_content('2 errors')
|
||||
expect(page).to have_content('Only integer numbers', count: 2)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user