所以我试图让client_side_validations
gem工作,当我从我的电子邮件字段中删除时,我收到此错误:
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="✓" />
<input name="authenticity_token" type="hidden" value="9JdqaiushdauhsdioauhdoiuhNLSAVCGrc+PLJAQ=" />
<input data-validate="true" id="form-email" input_html="{:autofocus=>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
...我仍然会收到此错误。
答案 0 :(得分:2)
所以我找到了解决方案,似乎client_side_validations
或devise
正在改变表单的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| %>
我发现了solution here。
答案 1 :(得分:0)
查看以下#263 Client Side Validations。这应该有所帮助。