jquery - Form validity constantly returns true regardless of validator in the model -
i'm using jquery hide , disable submit button until form has been validated paperclip's validates_attachment_file_name
it showing button form clicked on , continued show button regardless of file selected.
i called .checkvalidity()
on form , found out returning true. once when form clicked on , again when (any) file selected.
ruby model:
has_attached_file :file validates_attachment_file_name :file, matches: [/string\z/]
jquery:
$('#form').bind('change click', function() { if ($(this).valid()) { $('#submit').attr('disabled', false); $('#submit').show(); } else { $('#submit').attr('disabled', true); $('#submit').hide(); } });
what missing?
where's ajax?
you're calling javascript
function validate ruby
value.
as should aware, javascript client-side, ruby server-side. js can read what's literally printed in "dom" (document object model) ((html)).
thus, js false:
$('#form').bind('change click', function() { // on form change (not file input) if ($(this).valid()) { // if form valid $('#submit').attr('disabled', false); // enable submit $('#submit').show(); // show submit } else { $('#submit').attr('disabled', true); //disable submit $('#submit').hide(); //hide submit } });
the above deals form. need deal submission of form, , response. example here: http://firststopcosmeticshop.herokuapp.com (click "login", submit , see returned data)
the way fix issue send data server "ajax", taking response , manipulating dom it.
--
you'll best doing this:
#app/assets/javascripts/application.js $(document).on("submit", "#form", function(e){ e.preventdefault(); $(this).find("submit").attr('disabled', true); $.ajax({ url: $(this).attr("action"), data: $(this).serialize(), success: function(data){ // when successful }, error: function(data) { // validation errors show } }); });
you'll have appropriate controller action:
#app/controllers/your_controller.rb class yourcontroller < applicationcontroller def create @object = object.new object_params @object.save # conditional response here end end
-
now, have added problem of javascript being unable upload file on xml. there's technology resolves (jquery-file-upload
), used here (you'll have sign , go profile upload pic):
i can go more detail if required.
essentially, have replace above ajax functionality .fileupload
:
$('#avatar').fileupload({ url: '/profile/' + $(this).attr('data_id'), datatype: 'json', type: 'post',
Comments
Post a Comment