3

所以我试图让client_side_validationsgem 工作,当我从电子邮件字段中跳出时,我收到了这个错误:

Uncaught TypeError: Cannot read property 'user[email]' of undefined 

这是我的表单助手:

<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true) do |f| %>
      <%= f.email_field :email, :id => "form-email", :placeholder => "your-email@address.com", :input_html => {:autofocus => true}, :validate => true %>                
      <%= f.submit :label => "Submit", :value => "Sign Me Up!"  %>
      <div class="ajax-response"><%= devise_error_messages! %></div>
<% end %>

生成此 HTML:

<form accept-charset="UTF-8" action="/users" class="new_user" data-validate="true" id="new_user" method="post" novalidate="novalidate">
     <input name="utf8" type="hidden" value="&#x2713;" />
     <input name="authenticity_token" type="hidden" value="9JdqaiushdauhsdioauhdoiuhNLSAVCGrc+PLJAQ=" />
     <input data-validate="true" id="form-email" input_html="{:autofocus=&gt;true}" name="user[email]" placeholder="your-email@address.com" size="30" type="email" />
     <input label="Submit" name="commit" type="submit" value="Sign Me Up!" />
     <div class="ajax-response"></div>
</form>

编辑 1:即使我将表单助手从f.email_field :email, 更改为f.text_field :email...我仍然收到此错误。

4

2 回答 2

2

所以我找到了解决方案,似乎要么client_side_validationsdevise正在改变id表单 - 这会破坏 JS 验证。

所以解决方案是强制执行id表单,所以我的form_for助手现在看起来像这样:

<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true, :html => { :id => 'user_new' }) do |f| %>

我在这里找到了这个解决方案

于 2012-06-06T18:08:20.903 回答
0

查看以下#263 客户端验证。这应该会有所帮助。

于 2012-06-06T08:09:27.700 回答