0

在 text_field 中添加 name 属性时,数据不会被提交,当我删除 name 属性时,它会被很好地提交,但 jquery 验证不起作用。我正在使用 jquery formvalidation.io 插件进行验证,我想在 text_field 中添加一个自定义名称属性以进行验证。到目前为止,我发现很难理解,我尝试在 text_field 中添加这样的“'name'=>'test'”,但没有用,希望有人能提出解决方案,以便 jquery 验证和数据提交都能正常工作,谢谢。

这是我的 HTML

<section class="content">
    <%= form_for(tbl_ad_test_overview, :html => {:class => "form-online", :id => "form", :style => "margin-left: 15px; width: 95%;"}) do |f|%>

            <div class="form-group">
                <%= f.label :test, 'Test Number *', class: 'font-color' %>
                <%= f.text_field :test, id: 'test', name:'test', placeholder: 'Test Number', class: 'form-control'%>
                <br>
            </div>

            <div class="form-group">
                <div class="col-lg-12 col-xs-offset-5">
                    <%= f.submit "Submit", id: 'validate', class: 'btn btn-primary'%>
                </div>
            </div>
    <% end %>
</section>

这是我的jQuery

$(document).ready(function() {      
 $('#validate').on('click', function(event) {   
  $('#form').formValidation({
    framework : 'bootstrap',
    icon : {
        valid : 'glyphicon glyphicon-ok',
        invalid : 'glyphicon glyphicon-remove',
        validating : 'glyphicon glyphicon-refresh'
    },
    fields : {
        test : {
            validators : {
                notEmpty : {
                    message : 'Please enter a positive number greater than 0'
                },
                stringLength : {
                    min : 1,
                    message : 'Please do enter at least 1 number'
                },
             }
           }
        }
     });
  });   
});

这是控制器中的必需参数

def tbl_ad_test_overview_params
    params.require(:tbl_ad_test_overview).permit(:test)  
end
4

2 回答 2

0

您需要首先以正确的方式编写验证脚本,例如使用以下示例代码...

$(document).ready(function() {
    $('#loginForm').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                validators: {
                    notEmpty: {
                        message: 'The username is required'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The username can only consist of alphabetical, number, dot and underscore'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    }
                }
            }
        }
    });
});

使用类似于上述格式的格式制作您的代码。如果您对自定义名称有疑问,请编写脚本并使用嵌入脚本解析的名称提交表单。从我更新的角度来看,名称不应该影响表单属性。

于 2017-07-11T07:50:36.493 回答
0

text_field(:post, :title, size: 20)

# => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />

form helpers在记录的帮助下生成默认名称form_for,并向表单助手添加自定义名称会覆盖默认名称。

您正在覆盖可能导致提交失败的默认名称(在您的情况下它将是tbl_ad_test_overview[test])。test

解决方案:

不要添加自定义名称,而是在 Jquery 验证中使用默认名称。

<%= f.text_field :test, id: 'test', placeholder: 'Test Number', class: 'form-control'%>

并在 Jquery 验证中使用"tbl_ad_test_overview[test]"而不是。test

于 2017-07-11T07:52:53.107 回答