简体   繁体   English

Ruby on Rails验证复选框的存在

[英]Ruby on Rails validate presence of checkboxes

I have the following form for the model below it: 下面的模型具有以下形式:

    = simple_form_for @mem, :url => signup_path, :html => {:id => 'signup_form'}, :method => :post do |f|

        = f.input :interest, label: "I would like to learn more about:", collection: [ ['Interest 1', 'Interest 1' ], ['Interest 2', 'Interest 2']], :as => :check_boxes
        %br
        = f.input :full_name, :as => :string, required: true
        = f.input :email, :as => :string, required: true
        = f.input :telephone, :as => :string, required: true

        = f.submit "Submit"

Model: 模型:

class User
    include ActiveModel::Validations
    include ActiveModel::Conversion
    extend ActiveModel::Naming
    attr_accessor :full_name, :email, :telephone, :interest
    validates :interest, presence: { :message => "Please select your interest."}

I am able to successfully validate the other 3 fields in the form however I am having trouble validating the presence of the checkbox field :interest . 我能够成功验证表单中的其他3个字段,但是无法验证checkbox field :interest的存在。 I want to ensure that the user selects either one or both of the options. 我想确保用户选择一个或两个选项。 Thanks for your help 谢谢你的帮助

You can try 你可以试试

acceptance: true 接受程度:是

in you model. 在您的模型中。

validates :interest, acceptance: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM