0

My form is little complex, everything is on landing page welcome/index.html.erb.

This form has two tabs: products and users.

Under products tab there are features like:

  • create new product
  • show list of products

For each product there are options like:

  • add user to product

When I click on create new product there is div open which has form for new product.

I tried using client_side_validation but no success (from google search it looks like this gem is not supported for rails 3.2.13).

How can I show validation error when I submit my form using jquery?

this.submit(function() {
  //$('#createPolicy').slideToggle();
  $.post(this.action, $(this).serialize(), null, "script");
  return false;
})
return this;

Thanks for your help.

siekfried
  • 2,964
  • 1
  • 21
  • 36
user588324
  • 310
  • 1
  • 6
  • 19
  • are you really doing an ajax call in http://stackoverflow.com/questions/6723334/submit-form-in-rails-3-in-an-ajax-way-with-jquery?rq=1 there is something like $.ajax({....})? if so you need register some kind of callback method which informs you about the result of your action and you need to react on it appropriately, maybe http://stackoverflow.com/questions/5306380/error-handling-with-ajax-in-rails-3 can help here – jethroo Jul 08 '13 at 20:41

1 Answers1

1

You don't need ajax POST at all. If submit function returns falase, page won't be reloaded.

$('#button').submit(function() {
  if($('input#field_1').val().length===0){
  $('.errors').text("Can't be blank") } 
  return false
  else if(#more conditionals) {

  } #...
  else{
return true}
})

This way validation works both user-side and server-side. If you want to do it through AJAX request, you should provide response of method on case true,which also needs to rework content of page, on case false, providing errors meassages. A lot of work to do. I believe request is needed only when your validation is based on DB data, such as uniqueness.

Joe Half Face
  • 2,303
  • 1
  • 17
  • 45