0
votes
<%= simple_form_for (@category), :validate => true do |f| %>
  <%= f.error_messages %>

  <ol class="formList">
        <%= f.hidden_field :user_id, :value => current_user.id %>

      <%= f.input :category, :size =>20, :title => "Enter Category Name"  %>
    <%= f.submit %><div class="load_preview_customer"><%= f.submit "preview", :style => 'width:100px;', data: {:disable_with=>"loading preview..."} %></div>
  </ol>
<% end %>

the validation working only when i click on the submit button not when tab out of the field, I have also generated the rails.validation file under the initializer directory, and added the assets for simple form in application.js also

3

3 Answers

1
votes

You have to add rails.validations.simple_form after rails.validations. Also you could either add as follows:

(<%= javascript_include_tag "application", "rails.validations", "rails.validations.simple_form" %>)

Or,

In app/assets/javascripts/application.js:

//= require rails.validations
//= require rails.validations.simple_form 

Then change javascript_include_tag to following in app/views/layouts/application.html.erb:

<%= javascript_include_tag "application" %>
0
votes

There is a lot at work in the process you describe, so adding more detail would be good. For instance: clarify the difference between what happened, and what you expected to happen here. Understanding how this gem functions is also important--perhaps it is working as it should. This kind of action is based on events your browser generates, and clicking a button is quite different than the blur event, for example. It's possible the gem only operates on certain events, in other words. Expand on the situation!

0
votes

I believe simple_form's error messages are just passing on Rails validation errors. It sounds like you want some client-side validation. Maybe you want to look into using some front-end js to match your rails validation requirements.

EDIT: I noticed in a comment you referred to a railscast episode you followed Take note! That's a three-year old episode. I saw in the comments of the railscast someone recommending this gem as a current alternative: https://github.com/kalkov/rails4_client_side_validations

It's not well supported either but you might have better luck if you're using rails 4.