简体   繁体   中英

Ruby on rails - Simple form not displaying errors for radio select. Other errors display fine

I have a user registration form (Devise) with 4 elements; Username, email, password and 'user type'. User type is a boolean and is displayed as a radio select on the form.

Errors for Username, email and password show no problem, but I get no errors showing if a user doesn't select one of the radio buttons. Validation IS in place, and the form won't send without one of the radio buttons selected, but I get no errors.

The form:

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
            <%= f.error_notification %>
        <div class="role-selector">
            <p class="type-sel-txt"><%= t('front.whattype') %></p>
            <label for="artistuser">
                <%= f.radio_button :artist, "1", :id => "artistuser" %>
                <span class="artistuser-sel"><%= t('front.bandm') %></span>
            </label>
            <label for="eventuser">
                <%= f.radio_button :artist, "0", :id => "eventuser" %>
                <span class="eventuser-sel"> <%= t('front.evento') %></span>
            </label>
        </div>
        <%= f.input :username, required: true, autofocus: true, placeholder: t('forms.login.user'), label: false %>
        <%= f.input :email, required: true, placeholder: t('forms.login.email'), label: false %>
        <%= f.input :password, required: true, placeholder: t('forms.login.password'), label: false %>
        <div id="passcheck"></div>
        <%= f.button :submit, t("forms.login.signup"), id: "register-btn" %>
    <% end %>

User.rb:

  validates_inclusion_of :artist, in: [true, false], on: :create

Registration works without a problem, my only issue is the error not showing. I'm not sure if it is necessary to paste any more of my code, but if so I'll update with whatever is required.

As opposed to simple form's f.input which handles all related tags besides the input itself (the label tag, the error message tag), f.radio_button handles only the input field, because it is actually a helper from ActionView , not simple form.

Simple form generates a span with the "error" class by default when a field has some errors attached. I guess you will have to render the span yourself, in a similar way as you did for the field labels:

<div class="role-selector">
  <p class="type-sel-txt"><%= t('front.whattype') %></p>
  <label for="artistuser">
     <%= f.radio_button :artist, "1", :id => "artistuser" %>
     <span class="artistuser-sel"><%= t('front.bandm') %></span>
  </label>
  <label for="eventuser">
     <%= f.radio_button :artist, "0", :id => "eventuser" %>
     <span class="eventuser-sel"> <%= t('front.evento') %></span>
  </label>

  <!-- show error for the artist attribute, if present -->
  <% if resource.errors['artist'].present? %>
    <span class="error"><%= resource.errors['artist'] %></span>
  <% end %>
</div>

I can't figure it whole out what's happening but you can try some hack kinda thing

<%= f.radio_button :artist, "1", :id => "artistuser", :selected => f.object.artist %>

and then you can check whether it is present or not

<% if f.object.artist.blank? %>
  Display Your Error Message

Just a rough idea but with little modification you can make it work, let me know if there's any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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