简体   繁体   中英

Client side validations with Devise

I am trying to use the client_side_validations gem with Devise to validate devise registrations form.

Validations work fine with everything else just not the Devise generated form.

I added the relevant :validate => true but the validations only work when I hit submit not on tab like they do on every other form.

<h2>Sign up</h2>
<hr />

<%= form_for(resource, :validate => true, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :username %>
  <%= f.text_field :username %></div>

  <div><%= f.label :email %>
  <%= f.email_field :email %></div>

  <div><%= f.label :password %>
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation %></div>

  <br />

  <div><%= f.submit "Sign up", :class => "btn btn-primary btn-large" %></div>
<% end %>

<%= render "links" %>

Argc, argv! I am using Rails 3.2.1, the latest release of the gem is incompatible with 3.2 hence the nightmare. Using 3.2.0.beta.2 fixes the problems. Thanks!

Try to put the :validates => true on your fields directly, like this :

<h2>Sign up</h2> 
<hr />

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>   
  <%= devise_error_messages! %>

  <div>
    <%= f.label :username %>   
    <%= f.text_field :username, :validate => true %>
  </div>

  <div>
    <%= f.label :email %>   
    <%= f.email_field :email, :validate => true %>
  </div>

  <div>
    <%= f.label :password %>   
    <%= f.password_field :password, :validate => true %>
  </div>

  <div>
    <%= f.label :password_confirmation %>   
    <%= f.password_field :password_confirmation, :validate => true %>
  </div>

  <br />

  <div>
    <%= f.submit "Sign up", :class => "btn btn-primary btn-large" %>
  </div> 
<% end %>

<%= render "links" %>

change the line

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

to

<%= form_for(@user, :url => registration_path(resource_name), :validate => true) do |f| %>

I haven't used client_side_validations gem extensively yet. But from the look of it, it needs to have data-validate="true" in the form (and form elements) tags.

Do you find it in the output html form like:

<form novalidate="novalidate" method="post" data-validate="true" action="/some_actions" >

If you don't find it, you might want to write your form_for like this:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), {:validate => true}) do |f| %>

Does it help?

要使用稳定版本,请使用支持rails 3.2.x的最新稳定版3.0.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM