Hi,
Correct me if I am wrong but there doesn''t seem to be an easy way to
add
validation outside the pre-built ones (validates_presence_of,
validates_lenght_of, etc.).
Ideally there should be something like:
Class Person < ActiveRecord::Base
validates :my_method, :on => create, :message => ''was not
valid''
def my_method(param)
false unless
param ...
true
end
end
Actually, you can use the validate_on_create and validate_on_update, or if it''s something more general/complex, you can put: def validate return true unless whatever == badthing end You may also want to take a look at validates_each, which accepts a block, and is handy for working with groups of values. --Wilson. On 9/27/05, Sergio Bayona <sergio-SZhfuDltjDxEfCMKe0UOsQC/G2K4zDHf@public.gmane.org> wrote:> Hi, > > Correct me if I am wrong but there doesn''t seem to be an easy way to add > validation outside the pre-built ones (validates_presence_of, > validates_lenght_of, etc.). > > Ideally there should be something like: > > Class Person < ActiveRecord::Base > validates :my_method, :on => create, :message => ''was not valid'' > > def my_method(param) > false unless > param ... > true > end > > end > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
How about:
validate {|record| record.errors.add "Field" if record.field.blank? }
On 9/27/05, Wilson Bilkovich
<wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Actually, you can use the validate_on_create and validate_on_update,
> or if it''s something more general/complex, you can put:
> def validate
> return true unless whatever == badthing
> end
>
> You may also want to take a look at validates_each, which accepts a
> block, and is handy for working with groups of values.
>
> --Wilson.
>
> On 9/27/05, Sergio Bayona
<sergio-SZhfuDltjDxEfCMKe0UOsQC/G2K4zDHf@public.gmane.org> wrote:
> > Hi,
> >
> > Correct me if I am wrong but there doesn''t seem to be an easy
way to add
> > validation outside the pre-built ones (validates_presence_of,
> > validates_lenght_of, etc.).
> >
> > Ideally there should be something like:
> >
> > Class Person < ActiveRecord::Base
> > validates :my_method, :on => create, :message =>
''was not valid''
> >
> > def my_method(param)
> > false unless
> > param ...
> > true
> > end
> >
> > end
> >
> >
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>