Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleForm+ClientSideValidations+bootstrap - Validation is not occuring on the form- all inputs are accepted

I am using the SimpleForm, ClientSideValidations, and ClientSideValidations-SimpleForm gems in a RoR application. I can render the form into the modal beautifully, however when the input loses focus no validation occurs and the form is submitted. Also note that I have tried using the fix found here: http://www.ddarrensmith.com/blog/2012/05/17/ruby-on-rails-client-side-validation-with-validation-helpers-and-twitter-bootstrap/

Segment of the view containing the form:

<div class='modal hide' id='New_Discrep' tabindex="-1" role='dialog' aria-labelledby="#New_Discrep_Label" aria-hidden='true'>
  <div class="modal-header">
    <button type='button' class='close' data-dismiss='modal' aria-    hidden='true'>x</button>
    <h3 id="New_Discrep_Label">Create Discrepancy</h3>
  </div>
  <%= simple_form_for @new_discrepancy, :validate => true, :url => {:controller=> 'discrepancy', :action => 'create', :device_id => @device.id} do |f| %>
  <div class='modal-body'>
    <%= f.input :system, :wrapper => :prepend, :label => false do %>
      <%= content_tag :span, 'System', :class => 'add-on input-label' %>
      <%= f.input_field :system %>
    <% end %>
    <%= f.input :description, :wrapper => :prepend, :label => false do %>
      <%= content_tag :span, 'Description', :class => 'add-on input-label' %>
      <%= f.input_field :description, :class => 'textarea' %>
    <% end %>
    <%= f.input :conditions, :wrapper => :prepend, :label => false do %>
      <%= content_tag :span, 'Condiditions', :class => 'add-on input-label' %>
      <%= f.input_field :conditions, :class => 'textarea' %>
    <% end %>
    <%= f.input :dirf, :wrapper => :prepend, :label => false do %>
      <%= content_tag :span, 'Reference', :class => 'add-on input-label' %>
      <%= f.input_field :dirf %>
    <% end %>
  </div>
  <div class='modal-footer'>
    <button type="button" class='btn', data-dismiss='modal', aria-hidden='true'>Cancel</button>
    <%= f.button :submit, :class => 'btn-primary' %>
  </div>
  <% end %>
</div>

My Gemfile

gem 'rails', '3.2.1'
gem 'jquery-rails'
gem 'carrierwave'
gem 'simple_form'
gem 'client_side_validations'
gem 'client_side_validations-simple_form'

Model

class Discrepancy < ActiveRecord::Base
STATUSES = ['Open', 'Closed', 'Rejected']

  belongs_to :device
  belongs_to :user
  has_many :parts
  has_many :updates

  validates :date_entered, :presence => true
  validates :status, :presence => true
  validates :description, :presence => true
  validates :system, :presence => true
  validate :close_date

  validates_inclusion_of :status, :in => STATUSES, 
    :message => "must be one of: #{STATUSES.join(', ')}"  

end

application.js file

//= require jquery
//= require jquery_ujs
//= require rails.validations
//= require rails.validations.simple_form
//= require_tree .

I have also confirmed that the script tag is being produced in the html directly below the form. Thanks for the help!

like image 922
Ethan Stephens Avatar asked Oct 25 '12 16:10

Ethan Stephens


People also ask

What is the difference between HTML form validation and bootstrap form validation?

HTML form validation is applied via CSS’s two pseudo-classes, :invalid and :valid. It applies to <input>, <select>, and <textarea> elements. Bootstrap scopes the :invalid and :valid styles to their parent .was-validated class, usually applied to the <form>.

Is it possible to do client side validation with Bootstrap 5?

Since jQuery is no longer required for Bootstrap 5, it's easy to do client-side validation with vanilla JavaScript. The docs include a generic code example that should work on all forms with needs-validation ..

How to use bootstrap validation with custom error message using jQuery?

Bootstrap validation with custom error message by using jQuery. The form validation in Bootstrap. In HTML 5, the default form validation is done for the fields marked with the required attribute. It will even check the email format for the field specified as type=”email” with the required attribute.

What is Bootstrap invalid and valid bootstrap?

Bootstrap scopes the :invalid and :valid styles to their parent .was-validated class, usually applied to the <form>. Otherwise, any required field without a value shows up as invalid when a page is loaded.


2 Answers

All the logic gets applied to forms with visible inputs. Since your inputs are not visible on page load, no events are attached.

The following worked for me:

$('#myModal').on('shown', function () {
  $(ClientSideValidations.selectors.forms).validate();
});
like image 194
Tobscher Avatar answered Oct 28 '22 13:10

Tobscher


The README actually details a more specific function to do this:

$(new_form).enableClientSideValidations();

https://github.com/bcardarella/client_side_validations#enabling

like image 39
Michael Yagudaev Avatar answered Oct 28 '22 12:10

Michael Yagudaev