Forum: Ruby on Rails :before_filter validate for fields not part of model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David (Guest)
on 2009-04-24 00:45
(Received via mailing list)
I have created a pre_validate method and put it in :before_filter in
order to make sure that a custom javascript pulldown has been
selected.  I am using a boolean variable as a flag that turns to true
when a value is selected and I pass this variable using :with and
submit_to_remote:

  def pre_validate
    respond_to do |format|
      format.js do
        render :update do |page|
          if params[:time_selected] == 'false'
            page.replace_html 'error', "You must select a time."
          end
        end
      end
    end
  end

This way i can render the error using ajax.  If there are seemingly no
problems with this route, my question is this: I also have a custom
validation method in my model to check if there is a time conflict,
but this only gets called after I try object.save, so I was wondering
if it there are any other disadvantages to putting this custom
validation into a similar :pre_filter function other than only being
able to render one error at once.  It seems like it might be a waste
to have to create an object only to have it fail.  Why not check if it
fails in this particular instance first?
This topic is locked and can not be replied to.