SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Location
    Eastbourne, UK
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validation for form_remote_tag

    Hi,

    I've just been playing around with some of the builtin ajax capabilities of rails but I've run into a bit of a roadblock.

    So far I've got a drag-n-drop sortable list working with a remote form that adds new items and reloads the list so that the new item is sortable. This all works fine up until a user enters something in the remote form that hits a validation rule!

    Has anyone worked out a simple way of handling validation errors for ajax requests?

    The only solution I've found so far is here: http://www.bigsmoke.us/ajax-validation-on-rails/ but that looks over complicated and not very rubyish

    Kev

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use a simple controller action to handle inline AJAX edits - as well as sending the updated value to the server, I also send the model name and the attribute thats being updated and the ID of the record. You can then load the record using the class name and the ID, update the specified attribute with the given value, then check if that single value is valid. If it is, then you can call save(false) which will bypass validations for the entire object (as you only care about the attribute being edited). I can't remember the code I use off of the top of my head but its something like this:

    Code:
    def process_ajax_edit
      @obj = eval(params[:klass].camelize).find(params[:id])
      @obj.send("#{params[:attr]}=", params[:value])
      @obj.valid?
      if @obj.errors.invalid?(params[:attr])
        render :text => @obj.errors.on(params[:attr]).to_s, :status => 500
      else
        @obj.save(false)
        render :text => @obj.send(params[:attr]).to_s
      end
    end
    You can handle the error message in the AJAX onFailure callback (which is why we send a 500 status with the error message) - I find a simple Javascript alert with the error suffices, its much less hassle than putting it in your UI somewhere.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    www.ajaxscaffold.com has an interesting solution.

    they have a <div id="messages">...</div> that is usually hidden. this allows you from the server to do stuff like this.


    Code:
    def open_version_detail
        begin
          @brief = Brief.find_version params[:id], params[:version]
          @successful = true
        rescue
    	  flash[:error], @successful = $!.to_s, false
        end
    end
    and in the open_version_detail.rjs

    Code:
    @options = { :scaffold_id => params[:scaffold_id], :action => "version_detail", :id => params[:id], :version => params[:version] }
    @view_options = @options.merge(:action => "view")
    
    if @successful
    	page.hide element_row_id(@view_options)
    	page.insert_html :bottom, scaffold_tbody_id(@options), :partial => 'view_detail', :locals => { :hidden => true }
    	
    	page << "new TableRow.MoveAfter('#{element_row_id(@view_options)}', '#{element_row_id(@options)}');"  
        page.show element_row_id(@options)
    else
    	page.replace_html scaffold_messages_id(@options), :partial => 'messages'
    end
    
    page.hide loading_indicator_id(@view_options)
    the messages partial will handles the rest. red for error, purple and blue for the other 2


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
  •