2
votes

I have application rails and use client_side_validations gem with Devise to validate devise registrations form

//account.rb

has_one :user
validates :username, presence: true, uniqueness: true

//user.rb

 belongs_to :account
 validates :email, presence: true, uniqueness: true

//new.html.erb

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

   <%= f.fields_for :account, :validate => true do |inner_form|%>
      <div  class="field">
          <%= inner_form.label :username, "Username" %><br />
          <%= inner_form.text_field :username %>
      </div>
   <% end %>

   <div class="field">
       <%= f.label :email, "Email" %>
       <%= f.email_field :email %>
   </div>
   <div>
       <%= f.submit "Sign up" %>
   </div>

<% end %>

Validations work fine with email field but for username not worked.. help me,,, thanks

UPDATE ---validate show but this is no nested form

change :account to @account

<%= f.fields_for :account, :validate => true do |inner_form|%>

to

<%= f.fields_for @account, :validate => true do |inner_form|%>

when submit i get the error Account(#27129348) expected, got ActiveSupport::HashWithIndifferentAccess(#26695068)

when validates not worked, html code

<input id="user_account_attributes_username" name="user[account_attributes][username]" size="30" type="text" data-validate="true">

validates worked

<input id="user_account_username" name="user[account][username]" size="30" type="text" data-validate="true">

issue

user_account_attributes_username

user[account_attributes][username]

worked

user_account_username

user[account][username]

2
What didn't work? The validations aren't enforced, or error messages aren't shown? - Andrew
I think your main problem is the way you have set up your <%= f.fields_for :account. I am certain that you should have it set out like the following <%= fields_for @account, :validate => true do |inner_form|%> let me know if this resolves the issue. - user532339
@Andrew no show error message , but validation not generate.. - GeekToL

2 Answers

2
votes

For current DavyJonesLocker/client_side_validations version (Rails 4.2) fixing nested forms is just replacing line (in rails.validations.js or .coffee placed inside validateForm function):

name = name.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, "[][$1]");

With some better regexp, which will eliminate every [ids] and [new_ids] from name string. Here is one solution presented on jeroenj/client_side_validations:

name = name.replace(/\[((?:new_)?\d+|[0-9a-f]{24})\]/g, "[]");

It's nice (as long as you have sane attributes names) but probably there are some better ways to do this. If anyone is good at writing JS regexps please help to improve it. I will try as soon as I finish current project but I'm definitely not a regexp master.

IMPORTANT:

What is more in current client_side_validations version you have to comment out if(...) { } which surrounds mentioned regexp instruction. This is just quickfix, if is probably necessary at some point but I didn't find any usage for it yet.

1
votes

We found this branch which solved that problem.

I just updated my Gemfile with this line:

gem 'client_side_validations', github: "jeroenj/client_side_validations", :branch => "4-0-deeply-nested-forms"

Hope that work for you