Perform different field validation for the same model from 2 different controllers

ruby, ruby-on-rails, ruby-on-rails-3

Solution

As 23tux says, validations are based on the model. What I'd do is add a new column to your model:

add_column :products, :quick_registration, :boolean, default => true

Then in your User model:

class User < ActiveRecord::Base  
  validates :name, :presence => true
  validates :email, :confirmation => true
  validates :email_confirmation, :presence => true


  with_options :unless => :quick_registration? do |u|

    # These only validate during full registration
    u.validates :first_surname, :presence => true
    u.validates :prefered_language, :presence => true
    u.validates :dni, :presence => true, :uniqueness => true #, :format => {:with => /(^\d{7,8}[a-zA-Z]$)|(^[a-zA-Z]\d{7}[a-zA-Z]$)/, :unless => "dni.blank?"}
    u.validates :phone_number, :presence => true, :format => { :with => /^((\d{9})|([+]\d{11}))$/, :unless => "phone_number.blank?"}, :uniqueness => true
    u.validates :birthdate, :presence => true, :age => {:adult => :over}
    u.validates :legal_conditions, :acceptance => {:accept => true}
    u.validates :password_confirmation, :presence => true, :if => "new_record?"
    u.validates :country, :presence => true

  end
end

Then in your controller, you can 'switch on' the additional validations by setting `quick_registration` to false:

@user.quick_registration = false
@user.save   # all validations should fire

Problem

I am trying to generate a quick registration form to use Facebook, Twitter and Linked sign in on my app. But I do have user model that validates some fields on the previous sign up procedure. I am trying to extend the Registration controller, based on Devise, in order to use the same database for quick registration from the external sign in. This question is formulated because I am running a trial and then I have the sign up form asking for the credit card. I have this controller: ``` class Users::QuickRegistrationController < Users::RegistrationsController self.model_class = Users::RegistrationsController end ``` And then I have this user model: ``` class User < ActiveRecord::Base validates :name, :presence => true validates :first_surname, :presence => true validates :prefered_language, :presence => true validates :dni, :presence => true, :uniqueness => true #, :format => {:with => /(^\d{7,8}[a-zA-Z]$)|(^[a-zA-Z]\d{7}[a-zA-Z]$)/, :unless => "dni.blank?"} validates :phone_number, :presence => true, :format => { :with => /^((\d{9})|([+]\d{11}))$/, :unless => "phone_number.blank?"}, :uniqueness => true validates :birthdate, :presence => true, :age => {:adult => :over} validates :legal_conditions, :acceptance => {:accept => true} validates :email, :confirmation => true validates :email_confirmation, :presence => true validates :password_confirmation, :presence => true, :if => "new_record?" validates :country, :presence => true ``` Can I say something like validates :first_surname, :presence => false for the Users::QuickRegistrationController and :first_surname, :presence => true for Users::RegistrationsController? Additionally, there's a solution to solve this issue on Ruby, can you provide any tutorial or related link?

Original source