formValidation jquery plugin does not read rails safe form names

只愿长相守 提交于 2019-12-25 02:53:26

问题


I am using the formValidation plugin to validate my Rails 4 form. As per this question, I had to change my field "names" from 'name="firstname"' to 'name="user[firstname]"' so that the forms would submit correctly. However, the formValidation plugin relies on using the "name" of field as opposed to the "id" of the field to run the validation code. I can make the formValidation work if the name of a field is "firstname" but NOT if the name is "user[firstname]". Is there a way to write a "name" value in Javascript (newbie here) using brackets?

Here is my form. I've tried

<%= 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=>'user[firstname]', :class => 'form-control', :autofocus => true, :required => true,  :placeholder => 'FIRST NAME' %> 

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

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

  <%=    f.password_field :password, :class => 'form-control  ', :name=>'user[password]', :autofocus => true, :required => true,  :placeholder => 'YOUR PASSWORD' %> 
      <%=    f.password_field :password_confirmation,  :name=>'user[password_confirmation]', :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: {
            user[firstname]: {
                validators: {
                    notEmpty: {
                        message: 'Please enter your first name'
                    }
                }
            },

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


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

                }
            },

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

    // The disabled class
    disabled: 'disabled'
}

        }
    });
});
</script>

回答1:


According to the docs for your validator plugin,

If the field name contains special characters such as ., [, ], you must wrap it between single or double quote. See the Validating field with special name example

So just put your field names inside quotes, ""




回答2:


My original code wasn't using the correct format for naming "names"...but I couldn't debug that, as I was also missing a random comma right above "button". I'm posting the working code:

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

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

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


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

                }
            },

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

    // The disabled class
    disabled: 'disabled'
}

        }
    });
});
</script>


来源:https://stackoverflow.com/questions/30154511/formvalidation-jquery-plugin-does-not-read-rails-safe-form-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!