Real time form validation

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.

Hey @gus, here you can find a detailed guide on implementing various real-time features in your app.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs