I'm trying to stop simple_form from adding error labels entirely.
tried the followign CSS:
label.error { display:none; }
but simple_form's JavaScript is setting the following rule when it's generated:
display: block;
Am I missing a config that lets me turn off generation entirely?
This stops them from appearing, which works for now:
label.error {
display: none !important;
visibility:hidden;
}
Give this a try:
<%= f.input :password, error: false %>
Source # lib/simple_form/components/errors.rb
If you want to disable for ALL fields, I believe you'd have to put this on all fields.
You can also disable labels, hints or error or configure the html of any of them:
<%= simple_form_for #user do |f| %>
<%= f.input :username, :label_html => { :class => 'my_class' } %>
<%= f.input :password, :hint => false, :error_html => { :id => "password_error"} %>
<%= f.input :password_confirmation, :label => false %>
<%= f.button :submit %>
<% end %>
For further reference check the link below:
https://github.com/plataformatec/simple_form
If you want to disable error messages on inputs site-wide, you can set this easily in the initialiser config/initializers/simple_form.rb:
SimpleForm.setup do |config|
config.wrappers :default, class: :input,
# Comment this line!
#b.use :error, wrap_with: { tag: :span, class: :error }
end
end
You will no longer see the validation messages beside every input.
In Rails 5 do the following to remove the hint underneath the input field and label from above
<%= f.input :password, required: true, label: false, hint: false %>
Related
I want to set the name of the current user as the value for the commenter.
when i use: <%= f.input :commenter, :input_html => { :value => '<% current_user.username %>' }, label: false, disabled: true %> I get a syntax error.
Actually, I don't want to display the commentor on adding the comment; only on the listing.
Besides Simple_form, I use Devise.
You're openning the ERB tag twice in the same expression, hence the syntax error. Try :
<%= f.input :commenter, :input_html => { :value => current_user.username }, label: false, disabled: true %>
I have a enum definition for my User model.
class User < ActiveRecord::Base
enum program_of_study: [
:program_of_study_unknown, :program_of_study_cs, :program_of_study_ceg,
:program_of_study_is, :program_of_study_science,
:program_of_study_engineering, :program_of_study_fass,
:program_of_study_business, :program_of_study_others
]
end
And in simple_form I have:
<%= simple_form_for(locals[:user], wrapper: :horizontal_form, html: {class: 'form-horizontal'},
url: {action: (locals[:is_new] ? 'create' : 'update')}) do |f| %>
<%= f.error_notification %>
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: locals[:user].program_of_study %>
<%= f.button :submit, class: 'btn-success' %>
<% end %>
However, it seems that although the program_of_study of a user is 'program_of_study_science'(by checking in rails console), when I render the form, the shown select element still has 'program_of_study_unknown' as the displayed one. The correct one was not selected.
Instead of the enum I used the keys and it seems to work, have you tried that:
collection: User.program_of_studies.keys
I didn't have to specify a selected option. My code looks like this:
input :status, as: :select, collection: Venue.statuses.keys, include_blank: false
My solution in the end
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: User.program_of_studies[locals[:user].program_of_study] %>
How to put placeholder in rails simpleform association. I have tried a different ways but nothing works for me. The target code is:
<%= f.association :sex, :include_blank => true %>
If you are using Select2, you can include placeholders in select elements. The requirement is to include a blank element as first item and you can include the placeholder text as a data HTML attribute.
In your case:
<%= f.association :sex, include_blank: true,
data: { placeholder: 'Search...'} %>
Please check the simple_form.rb in initializers
You can find something below
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
.
.
.
config.wrappers :default, :class => :input, :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
## Extensions enabled by default
# Calculates placeholders automatically from I18n
# You can also pass a string as f.input :placeholder => "Placeholder"
b.use :placeholder
.
.
end
end
so it is depends on translational file
So you need to do for user model sex attribute
en:
simple_form:
placeholders:
user:
sex:
As placeholder works for input
Then you try this
<% f.association :user do |u| %>
<%= u.input :sex %>
<% end %>
And It should work
Using Rails 5.0.0.beta2 and Simple Form 3.2.1, this works for me:
<%= f.association :strands,
collection: Strand.order(label: :asc),
include_blank: "-- Select strands --"
%>
I had no luck using various permutations on :prompt and some of the other ideas that I found suggested here.
<%= f.association :sex, :include_blank => true, placeholder: "sex" %>
tell me how to configure simple_form. I would like to use the checkbox of semantic ui, but when I wrap checkbox in the class, he becomes active, that is, visually it is, but when you click the check box is not activated.
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
.ui.three.column.middle.aligned.relaxed.grid.basic.segment
.column
.column
.column
.ui.form.segment
#marg.field= f.label :email, 'Email'
#marg.field=f.input :email, placeholder: 'Email', autofocus: true, label: false
#marg.field= f.label :password, 'Пароль'
= f.input :password, :required => false, placeholder: 'Пароль', label: false
#marg.ui.toggle.checkbox
= f.input :remember_me, as: :boolean if devise_mapping.rememberable?
#marg= f.button :submit, 'Войти!', class: 'small ui blue submit button'
http://i.imgur.com/C8Wn4K9.png
Please try it
= f.input :remember_me, as: :boolean, boolean_style: :inline
A simpler way to do this is to create a custom simple form wrapper. Here is a blogpost which describes how to add the wrapper: http://pranavsingh.me/semantic-ui-simple-form-wrapper/
The above configuration will automatically add all the essential classes semantic form classes, and also adds wrappers to create proper checkbox fields. Below is an example:
= f.input :published, label: "Published?",
hint: "If you are not ready to go live yet, keep this site unpublished.",
wrapper: :ui_toggle_checkbox
Here's what I had to do (the accepted answer got me on the right track, but didn't fully fix the issue):
In the config/initializers/simple_form.rb file, change
config.boolean_style = :nested
to
config.boolean_style = :inline
This prevents Simple Form from wrapping the input tag in the label tag (essentially inserting a tag between the '.ui .checkbox' div and the input, which breaks Semantic's checkbox functionality).
In the app/assets/javascripts/application.js file:
$('.ui.checkbox').find('input').checkbox();
My form now displays checkboxes correctly and passes the appropriate value when submitted.
Here is another option (to just fix one checkbox) -
In a _form.html.erb partial:
<div class="inline field">
<div class="ui checkbox">
<%= my_form.input :my_input, label: 'My Label', as: :boolean, boolean_style: :inline %>
</div>
</div>
In the application.js file:
$('.ui.checkbox').find('input').checkbox();
I'm trying to set a label for the following line but I keep getting an error if I add label_method anywhere. label: is just ignored. How can I add a label for f.select?
<%= f.select :state_identifier, Location::STATE, { prompt: 'State', id: 'state' } %>
I tried the following but it doesn't format properly in form-horizontal, leaving no gap between the label and data.
<%= f.label :state_identifier, label: 'State' %>
This is because f.select is not a simple_form method and does not support :label
Something like this should work for you w/ simple form.
<%= f.input :state_identifier, :label => "State", :collection => ["a","b"], :input_html => {:id=>"state" } %>
Hope this helps.