views:

457

answers:

7

For example, if I have a user model and I need to validate login only (which can happen when validating a form via ajax), it would be great if I use the same model validations defined in the User model without actually instantiating a User instance.

So in the controller I'd be able to write code like

User.valid_attribute?(:login, "login value")

Is there anyway I can do this?

+3  A: 

Since validations operate on instances (and they use the errors attribute of an instance as a container for error messages), you can't use them without having the object instantiated. Having said that, you can hide this needed behaviour into a class method:

class User < ActiveRecord::Base
  def self.valid_attribute?(attr, value)
    mock = self.new(attr => value)
    unless mock.valid?
      return mock.errors.has_key?(attr)
    end
    true
  end
end

Now, you can call

User.valid_attribute?(:login, "login value")

just as you intended.

(Ideally, you'd include that class method directly into the ActiveRecord::Base so it would be available to every model.)

Milan Novota
A: 

To use your standard validation routines:


User.new(:login => 'login_value').valid?

If that does not work for you, build a custom class method for this:


class User < ActiveRecord::Base

  validate do |user|
    user.errors.add('existing') unless User.valid_login?(user.login)
  end

  def self.valid_login?(login)
    # your validation here
    !User.exist?(:login=> login)
  end
end
wvanbergen
A: 

I have gone with the custom class solution but I just wanted to make sure there was no better way

class ModelValidator
  def self.validate_atrribute(klass, attribute, value)
    obj = Klass.new
    obj.send("#{attribute}=", value)
    obj.valid?
    errors = obj.errors.on(attribute).to_a
    return (errors.length > 0), errors 
  end
end

and I can use it like

valid, errors = ModelValidator.validate_attribute(User, "login", "humanzz")

humanzz
A: 
class User < ActiveRecord::Base
  validates_each :login do |record, attr, value|
    record.errors.add attr, 'error message here' unless User.valid_login?(value)
  end

  def self.valid_login?(login)
    # do validation
  end
end

Just call User.valid_login?(login) to see if login itself is valid

aivarsak
A: 

An implementation of the 'valid_attribute' method you are suggesting:

class ActiveRecord:Base
  def self.valid_attribute?(attribute, value)
    instance = new
    instance[attribute] = value
    instance.valid?

    list_of_errors = instance.errors.instance_variable_get('@errors')[attribute]

    list_of_errors && list_of_errors.size == 0
  end
end
August Lilleaas
A: 

How about:

User.columns_hash.has_key?('login')

A: 

Thank you Milan for your suggestion. Inspired by it I created a simple module one can use to add this functionality to any class. Note that the original Milans suggestion has a logic error as line:

return mock.errors.has_key?(attr)

should clearly be:

return (not mock.errors.has_key?(attr))

I've tested my solution and it should work, but ofc I give no guarantees. And here's my glorious solution. Basically a 2-liner if you take away the module stuff.. It accepts method names as stings or symbols.

module SingleAttributeValidation

  def self.included(klass)
    klass.extend(ClassMethods)
  end

  module ClassMethods
    def valid_attribute?(attr, value)
      mock = self.new(attr => value)
      (not mock.valid?) && (not mock.errors.has_key?(attr.class == Symbol ? attr : attr.to_sym))
    end
  end
end
Timo Lehto