简体   繁体   中英

Client Side Validations success message on attribute with no validations?

I'm using the client_side_validations gem to validate a contact form. The gem displays error messages beside fields when they are invalid. I have also implemented a custom callback ( see the Github wiki ) to display a success message beside fields when the user has filled them with valid information.

// Define a callback to run whenever an attribute passes validations
clientSideValidations.callbacks.element.pass = function($element, callback) {
  console.log("Element passed", $element);
  // Allow clientSideValidations to do it's thing.
  callback();
  // Add a success message to give the user an ego lift.
  $element.closest('p').addClass('pass');
  $message = $('<span class="message">Great job!</span>');
  $element.after($message);
};

However I run into a problem when I have a field which has no validations at all

class Contact < ActiveRecord::Base

  attr_accessible :name, :phone_number

  validates :name, presence: true
  # I don't need to validate the phone number in any way.
end

In this case, when the user tabs through the phone number field, the pass callback never gets run. I've tried to use the after callback to do this instead but I don't seem to run either.

Is there a way I can force the callbacks to run on every field in my form?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM