Real time form validation

gus
Sun, 14 Sep 2008 06:13:53 -0000

Hello, I am stuck with a realtime form validation. I obtained
information on how to build real time form validaion on:

http://blog.andyamiri.com/2008/05/ajax-form-validation-for-rails.html

It has worked great.... but... until now I have not been able to
validate a password confirmation field for the form. The problem is
based on my limitation to modify the code to accept two different
field values to validate. I would appreciate it very much if someone
could help me. Thanks in advance.

Here is the code:

  def error_message_for(model, field, options = {})
    options[:success_css_class] ||= ''success''
    options[:error_css_class] ||= ''error''
    options[:hint_css_class] ||= ''hint''
    tag_id = "#{model}_#{field}_validator"
    js = observe_field "#{model}_#{field}", :function =>
      "new Ajax.Request(
        ''/usuario/validate?field=#{field}&value='' + value,
        { method: ''get'',
          onSuccess: function(transport) {
            element = document.getElementById(''#{tag_id}'');
            var output = transport.responseText;
            var css_class = ''#{options[:error_css_class]}'';
            if (output.length == 0) {
              output = ''#{options[:success]}'';
              css_class = ''#{options[:success_css_class]}''
            }
            element.innerHTML = output;
            element.setAttribute(''class'', css_class);
          }
        }
      );"
    tag = content_tag :span, options[:hint], :id => tag_id, :class =>
options[:hint_css_class]
    return tag + js
  end

This "helper" triggers the observe_field for changes... when the field
is changed it sends the "value" of the "field" to a function
called
"validate" which I show next:

def validate
    field = params[:field]
    user = User.new(field => params[:value])
    output = ""
    user.valid?
    if user.errors[field] != nil
      if user.errors[field].class == String
        output = "#{user.errors[field].slice(2,100)}"
      else
        output = "#{user.errors[field].slice(2,100)}"
      end
    end
    render :text => output
end

which then returns the result and gets displayed on the view... I need
to somehow add the "password_confirmation" field to the process so
that I can then validate that it is equal to the password. Thanks
again.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---