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
?
Related
Trying to make a boolean with a label left but it fails,
I could not find any complete example in the documentation online.
Currently Im using:
= f.input_field :remember_me, as: :boolean,
:input_html => { :checked => true },
:inline_label => true
= f.label "Remember me"
%br
= f.label "Remember me"
= f.input_field :remember_me, as: :boolean, :input_html => { :checked => true }, :inline_label => true
I work on app which have bootstrap modal(for sign in/registration) where client side validation is performed. When you try to sign in with regular, but non existing email in db, server side validation is performed and you are redirected to users/signin page.
So, instead of that redirection, I need to make validations on client side and show error message in modal. Problem is, I work on real, big app for first time and I cant find place where those validations and redirections are performed. I can't find any relevant JS file or controller actions..
User model:
validates :password, presence: true, :on => :create
validates :password, confirmation: true, :on => :create
validates :email, presence: true
validates :username, uniqueness: true, presence: true
validates_format_of :username, :with => /^[a-z\d]*$/, :message => "Just letters and numbers please"
Modal for registration:
#modal-registration.modal.hide.fade{"aria-hidden" => "true", "aria-labelledby" => "registrationLabel", role: "dialog", tabindex: "-1"}
.modal-header
%button.close{"aria-hidden" => "true", "data-dismiss" => "modal", type: "button"} ×
%h3#myModalLabel Registration
.modal-body
.row-fluid
.span6
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: "form", id: "registration-form" }, :validate => true) do |f|
.control-group
.controls
= f.input :email, autofocus: true, label: false, placeholder: 'email'
.control-group
.controls
= f.input :username, autofocus: true, label: false, placeholder: 'Username'
.control-group
.controls
= f.input :password, label: false, placeholder: 'Password'
.control-group
.controls
= f.input :password_confirmation, label: false, placeholder: 'Password confirmation'
.control-group
.controls
= f.button :submit, "Registration"
.span6
= link_to "Registration via Facebook", "#", class: "btn btn-large btn-block"
= link_to "Registration via LinkedIn", "#", class: "btn btn-large btn-block"
.modal-footer
%button.btn{"aria-hidden" => "true", "data-dismiss" => "modal"} Close
Client_side_validation jQuery:
jQuery ->
$("#modal-registration, #modal-sign-in").on "shown", ->
$(ClientSideValidations.selectors.forms).validate()
Used gems: devise, client_side_validations, client_side_validations_simple_form, bootstrap, haml.
Did you try rails bootstrap forms gem?
You can do client side validations with rails bootstrap forms gem easily
You only need to add remote: true code, for example:
= bootstrap_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :remote => true) do |f|
= f.email_field :email
= f.text_field :username
= f.password_field :password
= f.password_field :password_confirmation
= f.submit
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)
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.
How do I make a drop down menu reflect what's stored in it's corresponding column in a database?
I have a dropdown menu for gender selection and it updates the database fine
but goes back to default option in select menu on refresh where as all my text fields are pulling db data fine.
<%= form_for #profile, :remote => true, do |f| %>
Username: <%= #profile.user.username %><br />
URL: http://site.com/<%= #profile.user.username %><br />
First Name: <%= f.text_field :first_name, %><br />
Last Name: <%= f.text_field :last_name, %><br />
I am: <%= f.select :gender, options_for_select([['Select Gender', ''],['Male','m'],['Female','f']]) %><br />
<%= f.submit 'update' %><br />
<% end %>
Any clue what I'm missing?
Kind regards
Here's my model:
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessible :first_name, :last_name, :gender, :motd
# Local Variables
# Regex Variables
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
alpha_regex = /^[a-zA-Z]*$/
alpha_numeric_regix = /^[a-zA-Z0-9_]*$/
#Form Validation
validates :first_name, :presence => true,
:length => { :minimum => 2, :maximum => 15 },
:format => {
:with => alpha_regex,
:message => "Your first name must contain letters only"
}
validates :last_name, :presence => true,
:length => { :minimum => 2, :maximum => 15 },
:format => {
:with => alpha_regex,
:message => "Your last name must contain letters only"
}
validates :gender, :presence => true,
:inclusion => {
:in => %w( m f ), :message => "Are you male or female?"
}
end
Update method from controller
def update
respond_to do |format|
if #profile.update_attributes(params[:profile])
format.js { render :js => "window.location = '#{settings_edit_profile_path}'" }
flash[:success] = "Profile updated"
else
format.js { render :form_errors }
end
end
end
options_for_select has a special syntax for selecting a value:
<%= f.select :gender, options_for_select([['Select Gender', ''],['Male','m'],['Female','f']], "#{#profile.gender}") %>
might work like you expect.
Or you could create a Gender model and use collection_select which does this by default:
<%= f.collection_select :gender, Gender.all, :value, :description, :prompt => true %>