In my form, which is a credit card validation form, I have 2 fields expiry_year and expiry_month.
validates :expiry_month,
length: { is: 2, allow_blank: true },
numericality: { only_integer: true, allow_blank: true }
validates :expiry_year,
length: { is: 4, allow_blank: true },
numericality: { only_integer: true, allow_blank: true }
However I want to have a hidden Expiry field which shows as red in the form if the 2 fields above are not valid.
So the form will look something like this, but I need the Expiry label to be red in the validation, How can I do this?
Here is how the fields look in the form.html.haml for the view.
= f.label 'Expiry'
= f.text_field :expiry_month, placeholder: 'MM', maxlength: 2
= f.text_field :expiry_year, placeholder: 'YYYY', maxlength: 4
It seems you can do it like this:
If you have an error class you can check for any errors and display the class in the view.
.error { color: #9d1d20; }
- if #donation.errors.messages.keys.any? { |k| k.match(/expiry/) }
= f.label 'Expiry', class: "form-label-expiry error"
- else
= f.label 'Expiry', class: "form-label-expiry"
= f.text_field :expiry_month, autocomplete: "off"
= f.text_field :expiry_year, autocomplete: "off"
Related
I'm trying to only show errors in my form when the user clicks the submit button but currently, it is displaying all errors before the user clicks the submit button. How do I only show errors when the user submits the form?
I'm using simple-form in Rails
Here is my simple-form:
<div class="col-md-10 col-lg-8 col-xl-5 col-md-offset-4 mx-auto">
<%= simple_form_for #customer, url: customers_path, method: :post do |f| %>
<%= f.error_notification %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email, input_html: { autocomplete: 'email' } %>
<%= f.input :budget, collection: ["€200,000 - €299,999", "€300,000 - €399,999", "€400,000 - €499,999", "€500,000 - €649,999", "€650,000 - €799,999", "€800,000 - €1,000,000", "€1,000,000 +"] %>
<%= f.input :comments, :as => :text, :input_html => { 'rows' => 10, 'cols' => 10 } %>
<%= f.button :submit, "Submit", class: "btn-primary trigger mt-1" %>
<% end %>
</div>
Here are my customer validations in my customer model:
class Customer < ApplicationRecord
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, uniqueness: true
validates :first_name, presence: true, length: { minimum: 2 }
validates :last_name, presence: true, length: { minimum: 2 }
validates :budget, presence: true
validates :comments, presence: true
end
Thank you
you can change simple_form_for (line 2) like this below
<%= simple_form_for #customer, html: { novalidate: true }, url: customers_path, method: :post do |f| %>
explanation:
by adding , html: { novalidate: true } This option adds a new novalidate property to the form, instructing it to skip all HTML 5 validation.
As the code below shows, I am trying to validate an form value of an ActiveModel object based on the value of a checkbox of the same form.
If the box is checked (I made sure it will return true not 1) the validation on order_number should be deactivated, as the field is being deactivated as well (by JS). The naive approach shown below, using the attribute that is connected to the checkbox not_a_customer as conditional for the validation of order_number didn't work.
What else can I try?
I have an ActiveModel class:
class SupportCase
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor(:email, :os, :inquiry_type, :order_number, :first_name, :last_name, :message, :not_a_customer)
validates :order_number, presence: true,
numericality: { only_integer: true },
length: { in: (4..5), message: 'doh' },
unless: :not_a_customer
end
And a form for creating support cases:
= simple_form_for #support_case, html: { class: 'form inset' } do |f|
.row
.col.sm-6
.row{ id: 'order-row' }
.col.sm-6
= f.input :order_number, input_html: { class: 'icon-field hash-icon' }
.col.sm-6
.label-title{ title: t("simple_form.labels.support_case.hint") }
= f.input :not_a_customer, as: :boolean do
= f.check_box :not_a_customer, {}, "true", "false"
.col.sm-6
= f.input :email, input_html: { type: 'email', required: 'true' }
= f.input :first_name, input_html: { type: 'text' }
= f.input :last_name, input_html: { type: 'text' }
.col.sm-12
~ f.input :message, as: 'text', input_html: { required: 'true' }
%button.btn.btn-action
= t('views.contact.form.submit')
Check the value of not_a_customer. You are just getting a string instead of a boolean value, which is always truthy ("true", "false" are both truthy value).
In Rails, you can do not_a_customer.to_bool to convert it into boolean.
The checkbox does not convert your value into boolean, because params is parsed as if they are all strings (including int, string, boolean values).
I want to archive that the signup form give a validation error if the "accept terms" checkbox is not checked. for some reason the validation messages for all fields appear correctly but not for that checkbox.
users/new.html.erb:
<%= form_for(#user, url: signup_path) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.text_field :name, class: "login", placeholder: :name
...more fields...
<%= f.check_box :agreement, class: "field login-checkbox" %>
<label class="choice" for="Field"><%= t("agree_terms") %></label>
<%= f.submit t("register"), class: "button btn btn-primary btn-large" %>
<% end %>
models/user.rb:
class User < ActiveRecord::Base
validates :name, presence: true, length: { maximum: 50 }
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
# this is the validation
validates :agreement, acceptance: { accept: true }
...
end
Have a look here: http://guides.rubyonrails.org/active_record_validations.html#acceptance
validates :agreement, acceptance: true
should do the trick for you.
I have the following code in my model:
class Tweet < ActiveRecord::Base
validates_length_of :content, length: { maximum: 140 }
and the following validation in my controller:
def new
#tweet = Tweet.new
#maximum_length = Tweet.validators_on( :content ).first.options[:maximum]
end
And I get the following error message in my browser. I have no idea what's wrong with this!
Range unspecified. Specify the :in, :within, :maximum, :minimum, or :is option.
My view is:
<div class="form-group">
<%= form_for #tweet do |f| %>
chars left: <span id="counter" data-maximum-length = "<%=#maximum_length%>"<%= #maximum_length%></span>
<%= f.text_field :content, maxlength: #maximum_length, id: 'content' %>
<button type="submit" class="btn btn-primary">Submit</button>
<%end%>
<script>
$(document).ready(function(){
var content = $("#content");
var counter = $("#counter");
var max_length = counter.data("maximum_length");
content.keyup(function(){
counter.text(max_lnegth - $(this).val().length);
});
});
</script>
Use validates instead. This is the preferred method for all validations (view the Rails Validation Docs
validates :content, length: { maximum: 140 }
Using validates_length_of like you have in your code, it would be written as:
validates_length_of :content, maximum: 140
validates_length_of :content, maximum: 140
i want to know how to make a commbobox that shows a list of all users in my database, let me explain myself:
I have two clases:
class User < ActiveRecord::Base
validates :password, presence: true, confirmation: {strict: true}
validates :password_confirmation, presence: true
validates :telephone, uniqueness: true, presence: true, numericality: { only_integer: true }, presence: true, length: { minimum: 9, maximum: 9 }
validates :name, presence: true, length: { minimum: 4, maximum: 30 }, format: { with: /^[\w\s-]*/u, multiline: true,
message: 'only allows letters' }
has_many :valorations
end
class Valoration < ActiveRecord::Base
validates :points, presence:true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }
belongs_to :user
belongs_to :hability
end
And i have a show create view for the validation class:
<%= form_for(#valoration) do |f| %>
...errors check...
<div class="field">
#combo box code to be added
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And what i wanna do is to create a combo box that does something like the following pseudocode using fselect:
Invented code:
<%= f.select :user_id, options_for_select( for_each user do |u|
[[u.name,u.id]]
end %>
So at the end i have an combobox with all the users, i'm a noob to rails really know how to do it so any help is welcome
You can do something like below to achieve what you want.
<%= f.select :user_id, User.all.collect { |u| [ u.name, u.id ] } %>
For more info, you can refer to this link