Created
June 13, 2013 13:48
-
-
Save melnikaite/5773805 to your computer and use it in GitHub Desktop.
client-side-validations-formtastic
jasny bootstrap fileupload
image input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//conflict with ClientSideValidations | |
//this.$input.attr('name', this.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f.input :logo, :as => :image, :input_html => {:accept => 'image/*'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageInput | |
include Formtastic::Inputs::Base | |
def to_html | |
input_wrapping do | |
file_field = builder.file_field(method, input_html_options) | |
file_wrapper = <<-html | |
<div class="fileupload fileupload-new" data-provides="fileupload"> | |
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"></div> | |
<div> | |
<span class="btn btn-file"> | |
<span class="fileupload-new">#{I18n.t('formtastic.inputs.image.select_image')}</span> | |
<span class="fileupload-exists">#{I18n.t('formtastic.inputs.image.change')}</span> | |
#{file_field} | |
</span> | |
<a href="javascript:void(0);" class="btn fileupload-exists" data-dismiss="fileupload">#{I18n.t('formtastic.inputs.image.remove')}</a> | |
</div> | |
</div> | |
html | |
file_wrapper = file_wrapper.html_safe | |
label_html + file_wrapper | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
function validate_image(object) { | |
var parent = object.parents('.required'); | |
var element = parent.find('[type=file]')[0]; | |
var settings = window.ClientSideValidations.forms[element.form.id]; | |
$(element).isValid(settings.validators); | |
} | |
$(document).on('change', '.required [type=file]', function(){ | |
validate_image($(this)); | |
}); | |
$(document).on('click', '.required .btn.fileupload-exists', function(){ | |
validate_image($(this)); | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment