Skip to content

Instantly share code, notes, and snippets.

@jugyo
Created June 21, 2012 09:42
Show Gist options
  • Select an option

  • Save jugyo/2964874 to your computer and use it in GitHub Desktop.

Select an option

Save jugyo/2964874 to your computer and use it in GitHub Desktop.
# config/initializers/client_side_validations.rb
...
module SimpleForm
class FormBuilder
def self.client_side_form_settings(options, form_helper)
{type: 'SimpleForm::FormBuilder'}
end
end
end
// app/assets/javascripts/rails.validations.simple_form.js
/*
Client Side Validations - SimpleForm - v1.5.0.beta.3
https://github.com/dockyard/client_side_validations-simple_form
Copyright (c) 2012 DockYard, LLC
Licensed under the MIT license
http://www.opensource.org/licenses/mit-license.php
*/
(function() {
ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = {
add: function(element, settings, message) {
var errorElement, wrapper;
if (element.data('valid') !== false) {
wrapper = element.closest('.control-group');
wrapper.addClass('error');
$('.controls .help-inline', wrapper).remove();
errorElement = $("<span/>", {
"class": 'help-inline',
text: message
});
var helpBlock = $('.controls .help-block', wrapper);
if (helpBlock.size() == 0) {
return $('.controls', wrapper).append(errorElement);
} else {
helpBlock.before(errorElement);
}
} else {
return element.parent().find("span.help-inline").text(message);
}
},
remove: function(element, settings) {
var errorElement, wrapper;
wrapper = element.closest('.control-group.error');
wrapper.removeClass('error');
errorElement = wrapper.find("span.help-inline");
return errorElement.remove();
}
};
}).call(this);
# config/initializers/simple_form.rb
SimpleForm.setup do |config|
...
config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper :tag => 'div', :class => 'controls' do |ba|
ba.use :input
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
end
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment