2

I use client_side_validation gem and have question: in my client_side_validation.rb i have

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  unless html_tag =~ /^<label/
    %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
  else
    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
  end
end

and in output i have:

<div>
  Name:
   <br>
   <div class="field_with_errors">
     <input id="user_username" type="text" size="30" name="user[username]"data-validate="true">
     <label class="message" for="user_username">Only letters allowed</label>
   </div>
</div>

i don't want use label :

<label class="message" for="user_username">Only letters allowed</label>

how can i get somthing like this:

<div class="message">Only letters allowed</div>

i tried put in my rb file :

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  unless html_tag =~ /^<label/
    %{<div class="field_with_errors">#{html_tag}<div class="message">#{instance.error_message.first}</div></div>}.html_safe
  else
    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
  end
end

and restart server - but in this case i received only empty div with class message

help pls.. how change standart label to Div ?

0

1 Answer 1

2

There are two steps to overriding error messaging in client_side_validations, you either have to modify the ActionView::Base.field_error_proc or just the rails.validations.coffee (or JS).

For what you're trying to do, you don't need the field_error_proc changes. You can do it like this:

window.ClientSideValidations.formBuilders['ActionView::Helpers::FormBuilder'] = {
  add: function(element, settings, message) {
    $(element).after($('<div>').attr('class', 'message').html(message));
  },
  remove: function(element, settings) {
    $(element).next().remove();
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The message shown is three times <span class="message"> can't be left blank</span><span class="message"> can't be left blank</span><span class="message"> can't be left blank</span> and others classes are also removed which were before.
Sachin, try using window.ClientSideValidations.callbacks.form.fail which is the callback for the entire form. The add and remove methods for the FormBuilder are called for each field that does not validate correctly which is why you see multiple fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.