I am using mongoid with a model field and given validation:
field :status, type:String, :default=>'Active'
validates :status, :inclusion=>{:in=>%w(Active, Done, Canceled, Merged)}, :allow_nil=>true, :allow_blank=>true
in the form, I do not have the status field, so it's supposed to be not POST-ed therefore it's nil on creation:
= simple_form_for([#user, #task], :html => {:class=>'form-horizontal',:'data-type'=>'html'}) do |f|
- if #task.errors.any?
.error_explanation
.alert.alert-error
The form contains
= pluralize(#task.errors.count, 'error')
%ul
- #task.errors.full_messages.each do |msg|
%li=msg
.form-inputs
= f.error_notification
= f.association :project, :collection => current_user.projects.all
= f.input :description, :as => :text, :input_html => {:rows => 5}
= f.input :priority, :as=>:radio_buttons, :collection=>1..4, :item_wrapper_class=>'inline'
= f.input :due_date
.control_group.select.optional
= f.label :assigned_to, :class=>'select optional control-label', :for => 'assigned_to_id'
.controls
= f.collection_select :assigned_to_id, User.all, :id, :username, :class => 'select optional'
.form-actions
= f.button :submit, :class => 'form-button btn-primary', 'data-loading-text' => 'Submitting...'
however, I am still getting this despite setting a default value "Active", which is obviously in the array provided for the validation of inclusion:
Status is not included in the list
why am I still getting this error?
Thanks in advance!
This is your issue
%w(Active, Done, Canceled, Merged)
which translates to
["Active,", "Done,", "Canceled,", "Merged"]
solution is to remove the commas
%w(Active Done Canceled Merged)
Related
I have applied localization for a select box label as follows. Label option not working in collection_select in rails form. How can I change the code to get the label
Updated
= simple_form_for #product do |f|
= error_notification f
.form-inputs
.row
.span5
= f.input :name, :input_html => {:maxlength => 100}
.span5
- unless #product.company.nil?
= f.input :company_id, :as => :hidden, :input_html => { :value => #product.company.id}
- else
= f.association :company, :prompt => "Select Company"
.row
.span10
%div#product_existence_message
- unless #product.company.nil?
= f.collection_select :product_type_id, ProductType.all,:id, :name, {:prompt => 'Select Product Type', :label => :label => t('forms.products.label.name'), :selected =>#product.product_type_id }
File: en.yml
en:
activerecord:
attributes:
product:
product_type: 'Product Type translation here'
File: template.html.erb
<%= label(:product, product_type) %>
# => <label for="product_product_type">Product Type translation here</label>
Reference: https://apidock.com/rails/ActionView/Helpers/FormHelper/label[https://apidock.com/rails/ActionView/Helpers/FormHelper/label]
I have a simple_field and I pass an object to a partial file. I do this in the partial :
<%= f.input :address,
:as => :hidden,
:label => "Address :",
:input_html=>{
:required => false,
:class => "address"
} %>
I want to use :as => hidden depending on a certain condition. I mean the field should be hidden if a condition is true. Is that possible to do?
You could make your options hash separately and then add the hidden parameter if necessary
options = {
:label => "Address :",
:input_html=>{
:required => false,
:class => "address"
}
}
if condition_is_true? then options[:as] = "hidden" end
<%= f.input :address, options %>
I have a Rails form created with simple_form like so:
= simple_form_for #new_biz, url: new_business_post_path, html: {:class => "customForm1", :method => "post"} do |f|
= f.input :business_name, label: false, :input_html => {:value => #wat.business_name, :readonly => true}
= f.input :wat_id, :as => :hidden, :input_html => {:value => #wat.id}
= f.input :first_name
= f.input :last_name
= f.input :email
= f.input :phone, :label => "Phone number"
= f.label "Personal Message"
= f.text_area :message, label: false
= f.submit "Submit"
And validations:
validates :first_name, presence: {:message => "Please enter the first name."}
validates :last_name, presence: {:message => "Please enter the last name."}
validates :phone, presence: {:message => "Please enter the phone number"}
validates :message, presence: {:message => "Please enter a personal message"}
All validations display as expected except for the text_area field. I can't seem to find anyone else who has had this issue. All suggestions are greatly appreciated, thanks.
shouldn't your text field input be written as:
= f.input :message, as: :text, label: false
?
I have a form using rails and being submitted by angularjs. The problem I'm having is that when I try to access the form data from angular, the input that is not hidden will show up in the form newTask object, but the fields that are hidden won't. Is there something I am missing here?
= form_for [#patient, Task.new], html: {action: nil, name: 'newTaskForm', 'ng-submit' => 'taskCreate($event, newTask)'} do |f|
= f.hidden_field :taskable_id, :value => #patient.id,
'ng-model' => 'newTask.taskable_id'
= f.hidden_field :taskable_type, :value => #patient.class.to_s,
'ng-model' => 'newTask.taskable_type'
= f.hidden_field :creator_id, :value => current_user.id,
'ng-model' => 'newTask.creator_id'
= f.text_field :text, required: true, placeholder: 'Add a task...',
'ng-model' => 'newTask.text'
= f.submit 'Add task', 'ng-disabled' => 'newTaskForm.$invalid'
I've got an app that uses Devise and the ClientSideValidations gems.
In the form below, I've got client side validations working with devise. I think it's working since the it's now impossible to actually submit the form. My problem is that none of the text, such as "can't be blank" will appear next to the text field tags. Any idea what I'm doing wrong?
%h3 Sign up
%br
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true) do |f|
%h4
= devise_error_messages!
%div
= f.label :name
= f.text_field :name, :validate => true
%div
= f.label :email
= f.email_field :email, :validate => { :presence => :true }
%div
= f.label :password
= f.password_field :password, :validate => { :presence => :true, :length => true }
%div
= f.label :password_confirmation
= f.password_field :password_confirmation, :validate => { :presence => true }
%div
= f.submit "Sign up", id: 'button'
= render "devise/shared/links"
When I view source, the js appears.
<script>//<![CDATA[
if(window.ClientSideValidations==undefined)window.ClientSideValidations={};if(window.ClientSideValidations.forms==undefined)window.ClientSideValidations.forms={};window.ClientSideValidations.forms['new_user'] = {"type":"ActionView::Helpers::FormBuilder","input_tag":"<div class=\"field_with_errors\"><span id=\"input_tag\" /></div>","label_tag":"<div class=\"field_with_errors\"><label id=\"label_tag\" /></div>","validators":{"user[name]":{"presence":[{"message":"can't be blank"}]},"user[email]":{"presence":[{"message":"can't be blank"}]},"user[password]":{"presence":[{"message":"can't be blank"}],"length":[{"messages":{"minimum":"is too short (minimum is 6 characters)","maximum":"is too long (maximum is 128 characters)"},"allow_blank":true,"minimum":6,"maximum":128}]}}};
//]]></script>
So I know it there and I believe it's doing its job, but for some reason, I'm just not seeing the error messages I would expect when I leave a field blank.