SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date_select and validation

    I have a view that uses date_select. But the problem is that it let's the user choose invalid dates, such as June 31st.

    Does Rails have a simple way to validate dates of this type?

    Thanks!

  2. #2
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rgremill
    I have a view that uses date_select. But the problem is that it let's the user choose invalid dates, such as June 31st.

    Does Rails have a simple way to validate dates of this type?

    Thanks!
    By invalid do you mean not today (ie. the future)? Try something like this:
    Code:
    #model
      protected
      def validate
        self.errors.add(:created_on, "is invalid") if self.created_on > Time.now()
      end
    
    #controller
      def create
        @thing = Thing.new(params[:thing])
        if @thing.save
          flash[:notice] = 'Something was successfully created.'
          redirect_to :action => 'index'
        else
          flash[:error] = @thing.errors
          render :action => 'new'
        end
      end
    ... that'll stop the creation of @thing if the created_on date is greater than now. You could also use a before_save to validate the date before the item is created or updated.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    gregor002,

    The date_select creates multi part parameters that get turned into a date. If the user selects June 31 from the dropdowns, the code blows up because it is an invalid date. Some how I need to validate the fields before the multipart code runs and raise an exception. It looks like the multipart code runs before the create. But I could be wrong.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do Date.new(*invalid_parameters*), you can catch the exception with this code:

    Code:
    def youraction
      # set up date here
    catch ArgumentError
      # error handling
    end

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fenrir2,

    What Rails trigger do I use to catch this error? The error appears to happen before validate gets called.

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show me the code you're executing now, along with your model + table definitions and the error you're getting? (if there is an error).

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure,

    My table looks like this:

    create_table "feeds", :force => true do |t|
    t.column "title", :string
    t.column "description", :text
    t.column "start_date", :date
    t.column "end_date", :date
    t.column "key_number", :integer
    t.column "password", :string
    t.column "url", :string
    t.column "created_at", :timestamp
    t.column "updated_at", :timestamp
    end

    My Model looks like this:

    class Feed < ActiveRecord::Base
    validates_presence_of :title, :description, :start_date
    validates_format_of :url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix,
    :if => Proc.new { |feed| feed.url != '' }
    validates_confirmation_of assword

    before_create :generate_key_number

    def generate_key_number
    self.key_number = rand(9999999999)
    end

    end


    My View looks like this:

    <!--[form:feed]-->
    <p><label for="feed_title" class="FormText">TITLE*/label>
    <%= text_field 'feed', 'title', :class => 'FormInput', :size => '30', :maxlength => '40' %></p>

    <p><label for="feed_description" class="FormText">DESRIPTION*/label>
    <%= text_area 'feed', 'description', :class => "FormInput", :cols => '30', :rows => '5' %></p>

    <p><label for="feed_start_date" class="FormText">START DATE*/label>
    <%= date_select 'feed', 'start_date', :class => "FormInput", rder => [:month, :day, :year] %></p>

    <p><label for="feed_end_date" class="FormText">END DATE/label>
    <%= date_select 'feed', 'end_date', :class => "FormInput", rder => [:month, :day, :year] %></p>

    <p><label for="feed_end_date_comment" class="FormText"></label>
    <span class="FormCommentsBreak">LEAVE BLANK TO CREATE DAILY REMINDER</span></p>

    <p><label for="feed_password" class="FormText">PASSWORD/label>
    <%= password_field 'feed', 'password', :class => "FormInput", :size => "15", :maxlength => "10" %></p>

    <p><label for="feed_password_comment" class="FormText"></label>
    <span class="FormCommentsBreak">OPTIONAL. ENTER TO RESTRICT EDITS.</span></p>

    <p><label for="feed_password_confirmation" class="FormText">PASSWORD (REPEAT)/label>
    <%= password_field 'feed', 'password_confirmation', :class => "FormInput", :size => "15", :maxlength => "10" %></p>

    <p><label for="feed_url" class="FormText">LINK/label>
    <%= text_field 'feed', 'url', :class => "FormInput", :size => "30", :maxlength => "250" %></p>

    <p><label for="feed_url_comment" class="FormText"></label>
    <span class="FormCommentsBreak">E.G. http://www.colorcombos.com</span></p>

    <!--[eoform:feed]-->

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the controller, the error is happening when I create a new Feed object and the date_select fields are June 31st:

    class FeedsController < ApplicationController


    # def index
    # list
    # render :action => 'list'
    # end

    def index
    new
    render :action => 'new'
    end

    # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
    verify :method => ost, nly => [ :destroy, :create, :update ],
    :redirect_to => { :action => :list }

    def list
    @feed_pages, @feeds = paginate :feeds, er_page => 10
    end

    def show
    #@feed = Feed.find(params[:id])
    @feed = Feed.findfirst, :conditions => ["id = :id and key_number = :key_number",params])


    redirect_to :action => :list unless @feed


    end

    def new
    @feed = Feed.new
    end

    def create
    @feed = Feed.new(params[:feed])
    if @feed.save
    flash[:notice] = 'Feed was successfully created.'
    #redirect_to :action => 'list'
    # render :action => 'show'
    @feed.reload
    redirect_to :action => 'show', :id => @feed.id, :key_number => @feed.key_number
    else
    render :action => 'new'
    end
    end

    def edit
    #@feed = Feed.find(params[:id])
    @feed = Feed.findfirst, :conditions => ["id = :id and key_number = :key_number",params])

    if (@feed == nil)
    redirect_to :action => :list
    end

    end

    def update
    #@feed = Feed.find(params[:id])
    @feed = Feed.findfirst, :conditions => ["id = :id and key_number = :key_number",params])

    if (@feed == nil)
    redirect_to :action => :list
    end



    if @feed.update_attributes(params[:feed])
    flash[:notice] = 'Feed was successfully updated.'
    redirect_to :action => 'show', :id => @feed, :key_number => @feed.key_number
    else
    render :action => 'edit'
    end
    end

    def destroy
    Feed.find(params[:id]).destroy
    redirect_to :action => 'list'
    end
    end

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pastie is your friend.

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you get any errors?

    If so, you could use my catch-recipe for the create and edit actions.

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the error, it happens before the Create I think:

    ActiveRecord::MultiparameterAssignmentErrors in FeedsController#create

    1 error(s) on assignment of multiparameter attributes RAILS_ROOT: /Users/robertgremillion/workspace/reminderfeed/public/../config/..

    Application Trace | Framework Trace | Full Trace /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:2003:in `execute_callstack_for_multiparameter_attributes' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1983:in `assign_multiparameter_attributes' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1513:in `attributes=' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1354:in `initialize_without_callbacks' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:236:in `initialize' #{RAILS_ROOT}/app/controllers/feeds_controller.rb:37:in `create'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:2003:in `execute_callstack_for_multiparameter_attributes'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1983:in `assign_multiparameter_attributes'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1513:in `attributes='
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1354:in `initialize_without_callbacks'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:236:in `initialize'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
    /usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:600:in `each_cgi'
    /usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:597:in `each_cgi'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
    /Users/robertgremillion/workspace/reminderfeed/public/dispatch.fcgi:24
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:2003:in `execute_callstack_for_multiparameter_attributes' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1983:in `assign_multiparameter_attributes' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1513:in `attributes=' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1354:in `initialize_without_callbacks' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:236:in `initialize' #{RAILS_ROOT}/app/controllers/feeds_controller.rb:37:in `create' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue' /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!' /usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:600:in `each_cgi' /usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:597:in `each_cgi' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!' /Users/robertgremillion/workspace/reminderfeed/public/dispatch.fcgi:24

    Request

    Parameters: {"feed"=>{"end_date(3i)"=>"6", "title"=>"", "start_date(1i)"=>"2006", "start_date(2i)"=>"6", "password_confirmation"=>"", "url"=>"", "start_date(3i)"=>"31", "description"=>"", "end_date(1i)"=>"2006", "password"=>"", "end_date(2i)"=>"7"}, "x"=>"69", "y"=>"17", "event_start_date"=>"06/29/2006", "event_end_date"=>""}

    Show session dump

    --- flash: !map:ActionController::Flash::FlashHash {}
    Response

    Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}

  12. #12
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    def create
      # code that's in create now
    rescue ActiveRecord::MultiparameterAssignmentErrors
      # date invalid
    end

  13. #13
    SitePoint Evangelist
    Join Date
    Jun 2001
    Location
    Houston, Texas, USA
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fenrir2,

    I added the rescue and it appears to capture the error. At this point all I want to do is set the day fields to 1, flash an error and render the new view. The day parameter is named: feed[start_date(3i)]. How do I set this parameter value = 1?


    Here's what my code looks like:

    def create
    @feed = Feed.new(params[:feed])
    if @feed.save
    flash[:notice] = 'Feed was successfully created.'
    #redirect_to :action => 'list'
    # render :action => 'show'
    @feed.reload
    redirect_to :action => 'show', :id => @feed.id, :key_number => @feed.key_number
    else
    render :action => 'new'
    end

    rescue ActiveRecord::MultiparameterAssignmentErrors
    # date invalid
    flash[:error] = 'Date format was incorrect.'
    @feed = Feed.new(params[:feed])
    # redirect_to :action => 'new', params
    render :action => 'new'

    end

  14. #14
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do it like this if the day is in params[:feed][:day]:

    Code:
    @feed = Feed.new(params[:feed].merge({:day => 1})


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •