Sha256: 1dbc869d17543e6ce4b4e8d8243778a605fa48ba33298235cccf12c7e854c7b4

Contents?: true

Size: 1.9 KB

Versions: 6

Compression:

Stored size: 1.9 KB

Contents

var custom_validation_callbacks = {
  // Taglist inputs are built using the autoSuggest jQuery plugin, which dynamically generates markup to create a nice
  // facebook-like tag field. The plugin uses two inputs:
  //
  //   1) An input where the user types tag names. After clicking tab/comma, the plugin adds a new tag pill (`li`) to the
  //      list of tags (`ul`) and clears the input so the user can continue adding tags.
  //   2) A hidden input which contains the value all the tags as a comma separated list. Tags are automatically appended
  //      whenever a the user enters a new tag.
  //
  // Rails uses the second input when writing to the database. This happens automatically since both inputs have the same
  // `name` attribute, meaning only the second, hidden input gets passed along with the form parameters.
  //
  // Because of this markup, validation can be tricky since we really only care about the hidden input. Unfortunately,
  // the `client_side_validation` gem adds the validaiton behavior to the first input. This callback essentially overrides
  // the default behavior to ensure validations are ran against the second/hidden input.
  //
  taglist: function(element, event_data) {
    var tag_list_input = element.hasClass('as-input');
    if (tag_list_input) {
      var taglist_valid = $.trim( element.next().val() ).match(/[^,]/); // has at least one tag and it's not just whitespace
      var field = element.closest('.field');
      field.append('<span class="error"></span>'); // Cuase sometimes it doesn't have a error span. Why?

      if (taglist_valid) {
        element.data('valid', true);
        field.find('.error').text('');
      } else {
        element.data('valid', false);
        field.find('.error').text("can't be blank");
      }
    }
  }
};

clientSideValidations.callbacks.element.after = function(element, event_data) {
  custom_validation_callbacks.taglist(element, event_data);
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rostra-0.1.4 app/assets/javascripts/rostra/rails.validations.custom.js
rostra-0.1.3 app/assets/javascripts/rostra/rails.validations.custom.js
rostra-0.1.2 app/assets/javascripts/rostra/rails.validations.custom.js
rostra-0.1.1 app/assets/javascripts/rostra/rails.validations.custom.js
rostra-0.0.17 app/assets/javascripts/rostra/rails.validations.custom.js
rostra-0.0.16 app/assets/javascripts/rostra/rails.validations.custom.js