formValidation jquery plugin submitting a blank de

2019-08-19 00:08发布

问题:

I am using the formValidation query plugin for my Devise registration form. The plugin is from formValidation.io . I can get the form to register valid or invalid entries, but it always submits a blank form. For some reason the form is showing that it's filled out, but I keep getting errors saying that the required fields can't be blank. When I look at the trace, the sign up info IS being registered there...but it's getting lost somewhere along the line.

trace:

Started POST "/users" for ::1 at 2015-05-09 20:11:21 -0700
source=rack-timeout id=b008bd12bcf5f1f2687f7e216cf42136 timeout=20000ms service=1750ms state=active
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"uzEtfaHhjFg5M1aRL6shfpMxA3/Wbf/CVm5R1cU+NDNjrOx2nbnlkfQ9t+SlvKTk2Lm+F1r1rkvLfauEtFKRtA==", "firstname"=>"fred", "lastname"=>"flintstone", "email"=>"wilma@bedrock.com", "pw1"=>"dino123", "pw2"=>"dino123", "commit"=>"Create Account"}
   (0.3ms)  BEGIN
  User Exists (0.6ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('') LIMIT 1
   (0.2ms)  ROLLBACK
  Rendered users/registrations/_form.html.erb (5.9ms)
  Rendered users/registrations/new.html.erb within layouts/application (43.5ms)
source=rack-timeout id=b008bd12bcf5f1f2687f7e216cf42136 timeout=20000ms service=2006ms state=active
  Rendered layouts/_sidebar_signedout.html.erb (0.5ms)
Completed 200 OK in 237ms (Views: 206.9ms | ActiveRecord: 1.1ms)
source=rack-timeout id=b008bd12bcf5f1f2687f7e216cf42136 timeout=20000ms service=2108ms state=completed
source=rack-timeout id=4553e621abb4ef03750d71fd883738df timeout=20000ms state=ready
source=rack-timeout id=4553e621abb4ef03750d71fd883738df timeout=20000ms service=0ms state=active

form

<%= simple_form_for(resource, :as => resource_name,  id: 'new_user', :url => registration_path(resource_name)) do |f| %>

   <% if resource.errors.any? %>
<%= devise_error_messages! %>
<% end %>

<%=    f.text_field :firstname, :name=>'firstname', :class => 'form-control', :autofocus => true, :required => true,  :placeholder => 'FIRST NAME' %> 

<%=    f.text_field :lastname, :name =>'lastname', :class => 'form-control', :autofocus => true, :required => true,  :placeholder => 'LAST NAME ' %>         

 <%=    f.email_field :email, :name=>'email', :class => 'form-control ', :autofocus => true, :required => true,  :placeholder => 'YOUR EMAIL', :style=>"width:100%" %>

  <%=    f.password_field :password, :class => 'form-control  ', :name=>'pw1', :autofocus => true, :required => true,  :placeholder => 'YOUR PASSWORD' %> 
      <%=    f.password_field :password_confirmation,  :name=>'pw2', :class => 'form-control  ', :autofocus => true, :required => true,  :placeholder => 'CONFIRM YOUR PASSWORD' %>

 <%= f.submit 'Create Account',  :class => 'btn btn-aqua btn-lg btn-block', 
                                        :style => 'margin-bottom:5px' %>  


<%end%>


<script>
$(document).ready(function() {
    $('#new_user').formValidation({
        framework: 'bootstrap',

        icon: {
            valid: 'fa fa-check',
            invalid: 'fa fa-times',
            validating: 'fa fa-refresh'
        },
        fields: {
            firstname: {
                validators: {
                    notEmpty: {
                        message: 'Please enter your first name'
                    }
                }
            },

             lastname: {
                validators: {
                    notEmpty: {
                        message: 'Please enter your last name'
                    }
                }
            },


            email: {
                validators: {
                    notEmpty: {
                        message: 'The email address is required'
                    },
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    }
                }
            },
            pw1: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    },

                }
            },

              pw2: {
                validators: {
                    notEmpty: {
                        message: 'Please confirm your password'
                    },
                    identical: {
                        field: 'pw1',
                        message: 'The passwords do not match'
                    }
                }
            }
            button: {
    // The submit buttons selector
    selector: '[type="submit"]:not([formnovalidate])',

    // The disabled class
    disabled: 'disabled'
}

        }
    });
});
</script>

回答1:

Your issue is that you're setting the name attribute in all your form inputs. Don't do that.

Since you filled them with firstname, lastname, etc. your params look like

{"firstname"=>"fred", "lastname"=>"flintstone", "email"=>"wilma@bedrock.com", "pw1"=>"dino123", "pw2"=>"dino123", "commit"=>"Create Account"}

when they should look like

{"user" => {"firstname"=>"fred", "lastname"=>"flintstone", "email"=>"wilma@bedrock.com", "pw1"=>"dino123", "pw2"=>"dino123" }, "commit"=>"Create Account"}

Rails will automatically fill those with, for example, user[firstname], user[lastname], etc. which will make your params look something like: user: { firstname: 'blabla', lastname: 'something'}

If you need to manually set name, and your code expects a user object with attributes, you need to write it as name="user[attribute_name]"