I've been trying to get get some ajax validations going in my form and have been running into a bit of trouble
I have code like this in my form:
<% form_for @user, :html => { :id => 'user_form', :multipart => true } do |f| %>
<%= f.label :username %><br />
<%= f.text_field :username %> <%= error_message_on :user, :username %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %> <%= error_message_on :user, :email %>
</p>
#other form fields (password and confirmation)
Type the words below to prove you aren't a robot: <%= recaptcha_tags %>
<%= error_message_on :user, :base %>
<p><%= f.submit "Submit" %></p>
<% end %>
<%= observe_form "user_form", :url => users_path %>
This displays the errors fine if I don't use ajax, but I'm not sure how to make the "error_message_on" part to appear and disappear through ajax, through an onblur action if possible.
My controller works fine so I won't place the code here.
I was wondering if anyone could fill in the b开发者_StackOverflow中文版lanks here:
in validate.js.rjs:
#enables and disables the submit button on the bottom
if @user.valid?
page[:user_submit].enable
else
page[:user_submit].disable
end
page.select('.formError').each(&:remove)
#Some code to add the form error
#Some code to highlight the error field
Just to give you a visual idea of what I'm trying to do, here is my css:
.formError {
color: #D00;
font-size: 12px;
font-weight: bold;
}
.fieldWithErrors input {
border: 5px solid #f66;
}
My first piece of advice for you would be to ditch rails form helpers and start using formtastic (http://github.com/justinfrench/formtastic). I guarantee you satisfaction, or your money back.
After you've drunk the formtastic coolaid your next port of call should be validatious (http://github.com/grimen/validatious-on-rails/). It works really well with formtastic (or any form builder for that matter), and should help you avoid reinventing the wheel.
I hope this helps!
精彩评论