Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5: validation of acceptance for a checkbox

I have a sign-up form with some nested fields, and inside this form I have added a checkbox for the terms of service.

I'm trying to validate when the checkbox is checked or not and return an error if not.

 validates_acceptance_of :agreement, :allow_nil => true, :accept => true, :on => :create

I do have a boolean column that is set to default inside the accounts table.

add_column :accounts, :agreement, :boolean, default: false

Also, I have added the :agreement to the permitted controller params.

This is the form:

   <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <div class="centerList">
      <div id="error_explanation">
        <%= devise_error_messages! %></div>
    </div>

      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= f.email_field :email, autofocus: true, placeholder: "email", :class=>"form-control" %>
      </div>

      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= f.password_field :password, placeholder: "password", autocomplete: "off", :class=>"form-control" %>
      </div>

      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= f.password_field :password_confirmation, placeholder: "confirm password", autocomplete: "off", :class=>"form-control"  %>
      </div>

      <%= f.fields_for :account do |form| %>


      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= form.text_field :street, placeholder: "street", :class=>"form-control" %>
      </div>

      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= form.text_field :city, placeholder: "city", :class=>"form-control"  %>
      </div>

      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= form.text_field :state, placeholder: "state", :class=>"form-control" %>
      </div>

      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= form.text_field :zip_code, placeholder: "zip code", :class=>"form-control" %>
      </div>

      <div class="form-group">
        <span class="asterisk_input"></span>
        <%= form.country_select :country, ["US"], {}, { :class => "form-control", :id=>"sign-frm-input" } %>
      </div>

      <div class="form-group">
          <%= form.check_box :agreement %> I agree to the <%= link_to 'Terms', term_path(:id=>1) %>.
      </div>

<% end %>
      <div class="form-group">
        <%= f.submit "Sign up", :class=>'btn btn-primary' %>
  <% end %>

But it returns the form error agreement must be accepted even if I do accept it. Any ideas what I might be missing ohere!

like image 292
Theopap Avatar asked Jan 30 '18 13:01

Theopap


2 Answers

Try: validates :agreement, acceptance: {accept: true} , on: :create, allow_nil: false

You don't want to :allow_nil => true if you're validating for acceptance.

like image 170
tmee Avatar answered Nov 12 '22 02:11

tmee


You can change your model validations and test it.

validates :agreement, inclusion: {in: [true]}

or you can use other validation

validates :agreement, acceptance: { accept: true } 
like image 29
Sahidur Rahman Suman Avatar answered Nov 12 '22 02:11

Sahidur Rahman Suman