validate email format only if not blank Rails 3 - ruby-on-rails-3

I want to validate the email only if the email has been entered.
I tried the following:
validates :email, :presence => {:message => "Your email is used to save your greeting."},
:email => true,
:if => Proc.new {|c| not c.email.blank?},
:uniqueness => { :case_sensitive => false }
However this does not work as it stops error messages from showing when the email is left blank.
How can I validate the presence of the email when it is missing and ONLY validate the format when it has been entered?

This should do the trick.
validates :email,:presence => {:message => "Your email is used to save your greeting."}, :allow_blank => true,:uniqueness => { :case_sensitive => false }
Use:allow_blank => true or :allow_nil => true, :if => :email?
edit: fix typo.

You can write custom validation function:
class Model < ActiveRecord::Base
validate :check_email
protected
def check_email
if email.blank?
validates :email, :presence => {:message => "Your email is used to save your greeting."}
else
validates :email,
:email => true,
:uniqueness => { :case_sensitive => false }
end
end
end
or divide your validator into 2 separate validators with conditions:
validates :email,
:presence => {:message => "Your email is used to save your greeting."},
:if => Proc.new {|c| c.email.blank?}
validates :email,
:email => true,
:uniqueness => { :case_sensitive => false }
:unless => Proc.new {|c| c.email.blank?}

you can validate format if no precense required
:allow_nil => true

Related

Sign up page/form in Rails 3 not considering all fields required/mandatory

My sign up page has login. email, password, password_confirm and company fields . All of them are supposed to be mandatory but when i click on sign up it is not checking if any fields except password and password_confirm are blank!! I dont understand what is the difference as in my app/models/user.rb file i have validates presence for all fields. Please help me
Here's code(required lines)
user.rb
class User < ActiveRecord::Base
has_many :excel_files # One user may have many excel files
has_one :user_access_validity# One user may have one license period
# Virtual attribute for the unencrypted password
attr_accessor :password
attr_accessible :login
attr_accessible :email
attr_accessible :password
attr_accessible :password_confirmation
attr_accessible :company
#changes of 'validates' in accordance with rails 3:
validates :login, :presence => true,
:length => { :within => 3..40},
:uniqueness => { :case_sensitive => false },
:format => { :with => /^([a-z_0-9\.]+)$/i },
:on => :create,
:if => :is_login_entered?
validates :email, :presence => true,
:length => { :within => 7..100},
:uniqueness => { :case_sensitive => false },
:format => {:with => /^([a-z]+((\.?)|(_?))[a-z0-9]+#(mindtree.com|rvce.edu.in))$/i},
:on => :create,
:if => :is_email_entered?
validates :company, :presence => true,
:format => { :with =>/(mindtree|RVCE)/i},
:format => { :with => /^([a-z]+)$/i },
:on => :create,
:if => :is_company_entered?
validates :password, :presence => true,
:length => { :within => 4..40 },
:confirmation => true,
:format => { :with => /^([a-z0-9#!#\$]+)$/i },
:on => :create,
:if => :password_required?
validates :password_confirmation, :presence => { :if => :password_required? }
before_save :encrypt_password
#
# is_email_entered? : checks whether the email field is entered or not
# #params : none
# #return : true - if the email is entered
# false - if the email is not entered
#
def is_email_entered?
!self.email.blank?
end
#
# is_company_entered? : checks whether the company field is entered or not
# #params : none
# #return : true - if the company is entered
# false - if the company is not entered
#
def is_company_entered?
!self.company.blank?
end
#
# is_login_entered? : checks whether the login field is entered or not
# #params : none
# #return : true - if the login is entered
# false - if the login is not entered
#
def is_login_entered?
!self.login.blank?
puts "login"
end
protected
#
# password_required? : Checks whether either of the crypted_password and password field is blank
# #params : none
# #return : true - if either of the crypted_password and password field is blank
# false - if either of the crypted_password and password field is not blank
#
def password_required?
crypted_password.blank? || !password.blank?
puts "in pr?"
end
end
controller.rb
def signup
if logged_in?
flash[:notice] = "<span class='error'>You have already registered with us!</span>".html_safe
redirect_to :action => 'upload_file'
else
#user = User.new(params[:user])
return unless request.post?
#user.save!
self.current_user = #user
random_number = #user.generate_random_number
puts random_number
new_random_number = "c" + #user.id.to_s + random_number
#user.customerid = new_random_number
#user.created_at = get_current_datetime
# #user.updated_time = ''
#user.save
# save user's maximum access days
user_validity = UserAccessValidity.new
user_validity.user_id = self.current_user.id
user_validity.maximum_access_in_days = 90
user_validity.save
# redirect_back_or_default(:controller => '/account', :action => 'welcome')
redirect_to :controller => '/account', :action => 'welcome'
flash[:notice] = "<span class='success'>Thanks for registering!</span>".html_safe
end
end
signup.html.erb
<font color=red>(Fields marked * are mandatory)</font><h3>Sign me up!</h3>
<br>
<span class='error'><%= error_messages_for (#user) %></span>
<%= form_for :user do |f| -%>
<span class='error'><%= flash[:msg] %></span>
<p><label for="login"><span class='redcolor'>*</span>Login</label><br/>
<%= f.text_field :login %></p>
<p><label for="email"><span class='redcolor'>*</span>Email</label><br/>
<%= f.text_field :email %></p>
<p><label for="password"><span class='redcolor'>*</span>Password</label><br/>
<%= f.password_field :password %></p>
<p><label for="password_confirmation"><span class='redcolor'>*</span>Confirm Password</label><br/>
<%= f.password_field :password_confirmation %></p>
<p><label for="company"><span class='redcolor'>*</span>Company</label><br/>
<%= f.text_field :company %></p>
<p><%= f.submit 'Sign up', :name=> 'sign_up' %></p>
<% end -%>
The problem is that you pass an if option to your validation which also applies to the presence validation. Take for example the following
validates :login,
:presence => true,
:length => { :within => 3..40},
:uniqueness => { :case_sensitive => false },
:format => { :with => /^([a-z_0-9\.]+)$/i },
:on => :create,
:if => :is_login_entered?
is_login_entered? is called when validating the presence of login which conflicts with the validation which is why the validation is skipped. Change your validation to
validates :login, :presence => true, :on => :create
validates :login,
:length => { :within => 3..40},
:uniqueness => { :case_sensitive => false },
:format => { :with => /^([a-z_0-9\.]+)$/i },
:on => :create,
:if => :is_login_entered?
This way, the other validations will only run if a login is present.

Access all nodes in neo4j

On rails console i am trying to call
#invoice = Invoice.all
which returns me some object like
<Neo4j::Traversal::Traverser:0x817382>
but when i tried to loop over the object like
#invoice.each { |t| p t.number }
i got nil,but database contains data for invoice.
Invoice class
class Invoice < Searchable
include Neo4jrb::Paperclip
include LinkableEntity
include HeadsupNotify
enable_optimistic_locking
before_destroy :verify_links, :prepend => true
before_destroy :destroy_invoice_items
property :number, :type => :string
property :currency, :default => Money.default_currency.to_s
property :date, :pay_by_date, :type => :date
property :purchase_order_number
property :settle, :default => false
property :link_1, :link_2, :type => :string
index :number, :type => :fulltext
domain_property :details
money_property :total_cost, :receivable_amount
attr_protected :total_cost, :receivable_amount
attr_accessor :delink_receipt
validates :total_cost, :numericality => {:greater_than_or_equal_to => 0}
validates :receivable_amount, :numericality => {:greater_than_or_equal_to => 0}
validates :date,:number, :customer_id, :event_id, :project_id, :department_id,
:brand_id, :premise_id, :user_id, :currency, :presence => true
validates :link_1, :length => { :maximum => 250 }
validates :link_2, :length => { :maximum => 250 }
validate :number_uniqueness
validate :invoice_items_present
validate :invoice_items_currency
validate :issue_credit_notes_valid
validate :pay_by_date_valid, :unless => "date.nil?"
validate :check_not_linked, :on => :update
has_one(:event).from(Event, :invoices_for_event)
has_one(:project).from(Project, :invoices_for_project)
has_one(:department).from(Department, :invoices_for_department)
has_one(:brand).from(Brand, :invoices_for_brand)
has_one(:premise).from(Premise, :invoices_for_premise)
has_one(:user).to(User)
has_one(:customer).from(Customer, :invoices_for_customer)
alias :party :customer
has_n(:invoice_items).to(InvoiceItem)
has_n(:receipts).from(Receipt, :paid_invoices)
has_n(:issue_credit_notes).from(IssueCreditNote, :credit_notes_for_invoice)
has_one(:invoices_for_settle).to(Settle)
links :receipts, :issue_credit_notes,:invoices_for_settle
has_neo4jrb_attached_file :photo
{:customer => :name, :event => :name, :department => :name, :project => :name, :brand => :name,
:premise => :name, :user => :email}.each do |target, method|
delegate method, :to => target, :prefix => true, :allow_nil => true
end
accepts_id_for :customer, :event, :project, :department, :brand, :premise, :user
accepts_nested_attributes_for :invoice_items, :allow_destroy => true
validates_associated :invoice_items
validates :customer_name, :presence => true, :length => { :maximum => 100 }
validates :event_name, :presence => true, :length => { :maximum => 100 }
validates :premise_name, :presence => true, :length => { :maximum => 100 }
validates :project_name, :presence => true, :length => { :maximum => 100 }
validates :brand_name, :presence => true, :length => { :maximum => 100 }
validates :department_name, :presence => true, :length => { :maximum => 100 }
validates :link_1, :length => { :maximum => 250 }
validates :link_2, :length => { :maximum => 250 }
after_validation :set_total_cost
serialize :methods => :invoice_items
before_validation :delink
Can anyone please help me with this?
Thanks in advance.
I'm not exactly sure of how to do this with Ruby On Rails, but if your Invoice objects (nodes) are in a lucene full text index, you could use a Cypher query to return all Invoices.
Something like:
START invoices=node:Invoices('name: *') RETURN invoices;

rails-rspec uniqueness validation using accepts_nested_attributes_for

My models relationship like
class Cart < ActiveRecord::Base
has_many :cart_items, :inverse_of => :cart, :dependent => :destroy
accepts_nested_attributes_for :cart_items, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
validates_associated :cart_items
validates :cart_name, :presence => {:message => "Must be filled" }
end
class CartItem < ActiveRecord::Base
belongs_to :cart, :inverse_of => :cart_items
validates :cart_item_no, :presence => {:message => "Must be filled"}, :uniqueness => {:message => "Already exists" }
end
factories.rb
FactoryGirl.define do
factory :cart do
cart_name "sample"
factory :cart_with_cart_items do
after(:build) {|cart|
2.times { cart.cart_items.build(attributes_for(:cart_item)) }
}
after(:create) {|cart|
cart.cart_items.each {|cart_item| cart_item.save! }
}
end
end
factory :cart_item do
cart_item_no 1
cart
end
end
Below test case always fails
it "should not be valid with same cart_id" do
cart = FactoryGirl.build(:cart_with_cart_items)
cart.should_not be_valid
end
then I exploring in console, validation always returning true with same *cart_item_no* twice in cart_items by the example of below code.
cart = Cart.new(:cart_name => "yyy")
=> #<Cart id: nil, cart_name: "yyy">
2.times { cart.cart_items.build(:cart_item_no => 1000) }
=> 2
cart.cart_items
=> [#<CartItem id: nil, cart_item_no: 1000>, #<CartItem id: nil, cart_item_no: 1000>]
cart.valid?
=> true
cart.save!
=> true
Is this rails problem?
how to solve test case?
Uniqness validation checks against the database only. In your case, you have two cart_items that conflict with each other but not with any record in the database, so individually, both are valid, which is why your validations passes.
You could add a custom validation in your Cart class (not tested):
validates :unique_cart_items
def unique_cart_items
unless cart_items.collect(&:cart_item_no).uniq.size < cart_items.size
# set some error attribute here
end
end
This compares the number of unique cart_item_no values to the number of cart_items. If they are not the same, there's a duplicate cart_item_no.

rails 3 Validate presence based off boolean

I have a model that has a boolean called draft
I want to validate presence of fields only if draft == false.
my model
if self.draft == false
validates :name, :presence => true, :length => { :maximum => 45 }
validates :description, :presence => true
validates :blurb, :presence => true, :length => { :maximum => 175 }
validates :category_id, :presence => true
validates :location_id, :presence => true
validates :goal, :presence => true
else
end
in my controller
def new
#item.new(:draft => false) || #item.new(:draft => true)
def create
if params[:commit] == "Create Item"
#cause = Item.new(params[:item], :draft => false)
elsif params[:commit] == "Save Changes"
#cause = Item.new(params[:item], :draft => true)
end
It completely ignores my if statement and validates anyways whether it's true or not upon clicking Save Changes.
Suggestions would be greatly appreciated.
A conditional validation looks like below
validates :name, :presence => true, :length => { :maximum => 45 }, :if => :draft_is_false?
private
def draft_is_false?
draft == false
end
Similarly, you can perform conditional validations on other fields and these can be grouped as well. Go through the Conditional Validations sections on Ruby on Rails guides.
Here is a one liner in newer rails syntax:
validates :name, presence: true, length: { maximum: 45 }, if: -> (item) { item.draft? }
Notice that there is a question mark after the draft attribute. Rails automatically create methods like draft? for all attributes on a model, and is basically a shortcut for attr.present?. When used with Boolean attributes, it suggests that the developer's intention is to check for true/false.

Authlogic - how to set password_confirmation only for update?

I am trying to set up the password confirmation only on the page, where the user change his password.
My model looks this way:
class User < ActiveRecord::Base
attr_accessor :password_confirmation
acts_as_authentic do |c|
c.validate_login_field = false
c.validate_password_field = false
c.require_password_confirmation = true
c.logged_in_timeout(15.minutes)
end
validates :name, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => {:minimum => 3, :maximum => 40}, :on => :create
validates :email, :presence => {:message => 'address cannot be blank.'}, :allow_blank => true, :format => {:with => /\A[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]+\z/, :message => 'address is not valid. Please, fix it.'}, :uniqueness => true
validates :password, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => { :minimum => 6, :maximum => 40}, :on => :create
validates :password_confirmation, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => { :minimum => 6, :maximum => 40 }, :on => :update
end
and my method that saving new password:
def change_password
#user = current_user
if #user.valid_password?(params[:user][:old_password])
if #user.update_attributes(params[:user].reject{|key, value| key == "old_password"})
flash[:notice] = 'Your password was successfuly changed.'
redirect_to :back
else
flash[:warning] = 'You did not fill twice your new password correctly. Please, fix it.'
redirect_to :back
end
else
flash[:warning] = 'Your old password is WRONG! What is your malfunction!?!'
redirect_to :back
end
end
My problem is, that if I set the form the old password, then new password (eg. new_password) and then the confirmations of the new password (eg. new_password1), so the new password is changed & saved into the database - but it shouldn't, because the new password and the confirmation of the new password aren't the same...
How I should set up the validation rules or, where could be a problem?
Thanks for advices
You need to validate the password only if it's being changed. If it's not being changed, then the validation for the password field should be skipped.
Railscasts.com episode #41 shows you how to do this.