Rails: How to disable asterisk on form's required fields?
Asked Answered
V

11

42

When I add the 'Required' attribute
to html input fields, Rails pre-pends an asterisk (*) before the label.

Does anyone know how to prevent this?

For some reason Rails transforms this:

<%= f.input :Company, :input_html => {:value => "", :id => "company_name"}, :label => "company name" %>

into this:

<div class="input string required">
    <label for="company_name" class="string required">
    <abbr title="required">*</abbr> company name</label>
    <input type="text" value="" size="50" required="required" name="lead[Company]" id="company_name" class="string required">
</div>

I don't like that it wraps everything in a DIV and adds an ABBR element to the party.

How can I prevent this?

Val answered 4/10, 2011 at 15:56 Comment(4)
Can you please post the code that you are speaking about? That would help.Leadbelly
@Lester Peabody - I've updated the question with my code exampleVal
Are you using gems like formtastic or simple_form? Please post your Gemfile.Panathenaea
If you're using the formtastic gem, here is how to remove the asterisks.Garibald
J
35

In config/initializers/simple_form.rb add this line:

config.label_text = lambda { |label, required| "#{label}" }
Justiciary answered 2/1, 2013 at 19:3 Comment(8)
I had to both uncomment the line from the initializer field and change html: '' in the yaml file in my locales directory. FWIW, I am using simple_form with devise and bootstrap. Don't know or thinks that makes a difference thoughCornie
I've tried the locale solution and didn't work, but this one did. I'm using simple_form 2.0.4Demetra
Using 3.0-RC2, I used this: config.label_text = lambda { |label, required, explicit_label| "#{label}" }Pressurecook
Still not working for me. With devise 3.0.3, Rails 3.2.13, no chance, the config.label_text is completely ignored.Conall
Remember you need to restart your Rails server after editing an initializer file.Ripsaw
Is it possible remove the asterisk on a per-form basis?Emeldaemelen
@Emeldaemelen for that I just pass defaults: { required: false } as a form option.Annamariaannamarie
The following works with SimpleForm 3.5.0: lambda { |label, required, explicit_label| "#{label}" }. Note the inclusion of the explicit_label argument.Scowl
W
53

You can just set the required mark to empty value in simple_form's locale file:

en:
  simple_form:
    required:
      text: 'required'
      mark: '*'

Or use CSS to hide it.

Wellread answered 5/10, 2011 at 11:46 Comment(5)
would that create en empty html element? because that would be an highly unwanted result.Val
This doesn't work anymore - you now need to uncomment the "html" line underneath (or add it if you don't already have it), then set that to html: ''Zoogeography
This is the answer!, In case anybody else wonders, you should create a file name simple_form.en.yml under config/locales/Sandrasandro
No need to uncomment the html, but you do have to restart the server.Algie
You end up with whitespace using this approach: the <abbr></abbr> tag is still rendered.Neville
J
35

In config/initializers/simple_form.rb add this line:

config.label_text = lambda { |label, required| "#{label}" }
Justiciary answered 2/1, 2013 at 19:3 Comment(8)
I had to both uncomment the line from the initializer field and change html: '' in the yaml file in my locales directory. FWIW, I am using simple_form with devise and bootstrap. Don't know or thinks that makes a difference thoughCornie
I've tried the locale solution and didn't work, but this one did. I'm using simple_form 2.0.4Demetra
Using 3.0-RC2, I used this: config.label_text = lambda { |label, required, explicit_label| "#{label}" }Pressurecook
Still not working for me. With devise 3.0.3, Rails 3.2.13, no chance, the config.label_text is completely ignored.Conall
Remember you need to restart your Rails server after editing an initializer file.Ripsaw
Is it possible remove the asterisk on a per-form basis?Emeldaemelen
@Emeldaemelen for that I just pass defaults: { required: false } as a form option.Annamariaannamarie
The following works with SimpleForm 3.5.0: lambda { |label, required, explicit_label| "#{label}" }. Note the inclusion of the explicit_label argument.Scowl
L
22

I'm using Rails 3.1, and I have the following view code in my _form.html.erb for a given model:

<div>
  <%= f.label :full_name %><br/>
  <%= f.text_field :full_name, :required => true %><br/>
</div>

The label does not show an asterisk if you do it this way. Unless you post code I can't be sure of what your approach is and if my solution would fit said approach.

Updated Answer: It sounds like you've inherited this code from someone. At any rate, after reading your code sample, you are most definitely using the simple_form gem. Information about that gem can be found here https://github.com/plataformatec/simple_form. To answer your question though, if you change your code the following:

<%= f.input :Company, :input_html => {:value => "", :id => "company_name"}, :label => "company name", :required => false %>

That should turn off the asterisk.

I would add, based on your disgust for the HTML generated from simple_form, it sounds like you should just do away with the gem and re-write your form code using the Rails default form helpers, which can be read about here http://guides.rubyonrails.org/form_helpers.html. Depending on the size of the code base, you might be better off just sucking it up and learning how to use the simple_form gem for the sake of saving time, but if you think you have the time to change it all, go for it.

Leadbelly answered 4/10, 2011 at 16:32 Comment(6)
thanks, I will try splitting it. currently I use it as one line combined, maybe cause of that.Val
No problem, if this worked for you make sure to upvote and mark it as the accepted answer so other folks can benefit.Leadbelly
I don't understand, for some reason RoR thinks all fields are require by defaultVal
@vsync: I've updated my answer to reflect the code you provided in your updated answer. This should be all that you need, let me know if you need additional help.Leadbelly
I up-voted for this answer, and will have to verify it only next week when the other developer in my office comes back.Val
I figured you didn't downvote it as I was trying to help :) someone else did though... shakes fistLeadbelly
M
16

The simplest way is to hide it with this css:

abbr[title="required"] {
  display: none;
}
Moniquemonism answered 3/11, 2014 at 21:0 Comment(0)
K
5

It isn't rails at all. It's the simple_form gem. So, if you don't want all the wrapping elements don't use simple_form. Use Rails form helpers. It would be more simple than customize something you don't like.

Kiangsi answered 5/10, 2011 at 11:39 Comment(1)
But what if you want to use simple_form and just want to disable the asterisk? Most people who come to this question are probably using simple_form and want to keep itTourneur
G
4

For anyone using Formtastic and having this issue, you can remove the asterisks by editing the config file, which is typically app/config/initializers/formtastic.rb.

Change this line: # Formtastic::SemanticFormBuilder.required_string = "(required)"

to be: Formtastic::SemanticFormBuilder.required_string = ""

More info here.

Garibald answered 31/5, 2013 at 20:5 Comment(0)
S
3

Code that has helped me solve the asterisk issue:

abbr[title="required"] {
  display: none;
}

The chosen answer and the other suggestions asking to change the HTML in locales file dint help me with the latest Simple_form gem.

Selfloading answered 30/1, 2015 at 10:50 Comment(1)
where does one add this code, in a simple_form, bootstrap configuration? ThxLeatherette
A
3

Aside from the global config suggested in the accepted answer, you can pass required: false as an input option, or defaults: { required: false } to set it for the whole form.

Annamariaannamarie answered 5/9, 2017 at 22:20 Comment(3)
Others already said this in answers from 6 years ago.. you do not provide any new insights to this old matter..Val
Yes I did: how to set it for the whole form. My answer is also very brief, which is good.Annamariaannamarie
That is true, the part where you mention the defaults is indeed fresh informationVal
S
3

You can remove it from the whole form:

<%= simple_form_for @form, defaults: { required: false } do |f| %>
Spier answered 18/3, 2019 at 18:12 Comment(0)
P
0

I found out that if you only want to remove the asterisk(*) behind it then all you have to do is to go to this file file /config/locales/simple_form.en.yml

once again is not a good practice to change your configuration files for gems and something your using for some reason, it always a question of why do you really use simple_form!

But for example I found out about that because there is great things about simple_form we use but nowadays is a better usability practice to have the asterisks on none required fields then required ones.

Pegram answered 18/10, 2012 at 9:22 Comment(0)
A
0

you can used form_for, and override method def label in config/initializer to add asterisk for mandatory fields as the following:

def label(object_name, method, content_or_options = nil, options = nil, &block)
    if content_or_options.is_a?(Hash)

      content_or_options.each do |key, val|
        options[key] = val
      end

      content_or_options = method.to_s
    end

    content_or_options ||= method.to_s

    presence_validations = [ActiveModel::Validations::PresenceValidator, ActiveRecord::Validations::PresenceValidator]

    class_obj = options[:object].class if options[:object]
    class_obj ||= object_name.to_s.camelize.constantize

    validations = class_obj.validators_on(method.to_s).map(&:class)

    if (presence_validations.map { |pv| validations.include?(pv) }).any?
      content_or_options += "*"
    end

    Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
end

This method puts asterisk after all mandatory fields, if you used normal form_for, and used validates_presence_of

Anaemic answered 11/8, 2014 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.