rails validation plugin. returns true if at least one of the objects in the array is valid.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
README
init.rb

README

Validates at Least One
======================

When validating a has_many relationship, you might want to ensure at least one of the related items is valid.

Set :base_message => true and :message => "Blah blah" to display an error message for the entire object
Set :attribute_messages => true to display the individual attribute error messages.
Set :required_field to an attribute of your object, i.e. :name to display the base message if that field is blank and the attribute messages otherwise.

Example
=======

class Language < ActiveRecord::Base
	belongs_to :person
	validates_presence_of :name
end

class Person < ActiveRecord::Base
	has_many :languages

	validates_presence_of :name
	validates_at_least_one :languages, :message => "enter at least one language"
end

class Person < ActiveRecord::Base
	has_many :languages

	validates_presence_of :name
	validates_at_least_one :languages,
		:base_message => false, :attribute_messages => true,
		:message => "enter at least one language"
end

p = Person.new(:name => "Bob")
p.valid?   # false
p.languages << Language.new
p.valid?   # false
p.languages << Language.new
p.valid?   # false
p.language.first.name = 'en'
p.valid?   # true



Copyright (c) 2010 Paul Schreiber, released under the MIT license