From 627e3b7b5ffeb2d2a1afd27f681bdbe5d6b71dc7 Mon Sep 17 00:00:00 2001 From: Matt Barringer Date: Wed, 16 Jan 2013 08:22:20 +0100 Subject: [PATCH] Better form validation --- app/controllers/proposal_controller.rb | 29 +++++++++++++++++++------- app/models/event.rb | 5 ++++- app/models/person.rb | 3 ++- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/controllers/proposal_controller.rb b/app/controllers/proposal_controller.rb index 7da5eed4..1a66f5e1 100644 --- a/app/controllers/proposal_controller.rb +++ b/app/controllers/proposal_controller.rb @@ -1,9 +1,15 @@ class ProposalController < ApplicationController before_filter :verify_user + before_filter :setup before_filter :verify_access, :only => [:edit, :update, :destroy, :confirm] - def verify_access + def setup @person = current_user.person + @url = conference_proposal_index_path(@conference.short_title) + @event_types = @conference.event_types + end + + def verify_access if params.has_key? :proposal_id params[:id] = params[:proposal_id] end @@ -21,7 +27,6 @@ class ProposalController < ApplicationController end def index - @person = current_user.person @events = @person.proposals @conference end @@ -31,10 +36,7 @@ class ProposalController < ApplicationController end def new - @url = conference_proposal_index_path(@conference.short_title) @event = Event.new - @event_types = @conference.event_types - @person = current_user.person end def edit @@ -75,13 +77,26 @@ class ProposalController < ApplicationController submitter = params[:person] params[:event].delete :person + @event = Event.new(event_params) + @event.conference = @conference + + if submitter[:public_name].blank? + flash[:error] = "Your public name cannot be blank." + render :action => "new" + return + end + + if submitter[:biography].blank? + flash[:error] = "Your biography cannot be blank." + render :action => "new" + return + end + # First, update the submitter's info, if they've changed anything if submitter[:public_name] != person.public_name || submitter[:biography] != person.biography person.update_attributes(submitter) end - @event = Event.new(event_params) - @event.conference = @conference @event.event_people.new(:person => person, :event_role => "submitter") @event.event_people.new(:person => person, diff --git a/app/models/event.rb b/app/models/event.rb index 0c0a3f69..d4ae54a5 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -14,11 +14,14 @@ class Event < ActiveRecord::Base belongs_to :room belongs_to :conference - accepts_nested_attributes_for :event_people + accepts_nested_attributes_for :event_people, :allow_destroy => true accepts_nested_attributes_for :event_attachments, :allow_destroy => true, :reject_if => :all_blank accepts_nested_attributes_for :people before_create :generate_guid + validate :abstract_limit + validates :title, :presence => true + validates :abstract, :presence => true state_machine :initial => :new do state :new diff --git a/app/models/person.rb b/app/models/person.rb index 19882c36..eb07f1b6 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -4,6 +4,7 @@ class Person < ActiveRecord::Base has_many :event_people, :dependent => :destroy has_many :events, :through => :event_people, :uniq => true has_many :registrations, :dependent => :destroy + validates :first_name, :presence => true validates :last_name, :presence => true validate :biography_limit @@ -65,7 +66,7 @@ class Person < ActiveRecord::Base end def set_public_name - if public_name.empty? + if public_name.blank? self.public_name = "#{first_name} #{last_name}" end end