Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to put ActiveModel::Validator?

Tags:

I try to follow http://api.rubyonrails.org/classes/ActiveModel/Validator.html , but where should I put the

 class MyValidator < ActiveModel::Validator     def validate(record)       if some_complex_logic         record.errors[:base] = "This record is invalid"       end     end      private       def some_complex_logic         # ...       end   end 
like image 771
wizztjh Avatar asked Dec 17 '10 07:12

wizztjh


People also ask

What is the use of validator?

A validator is a computer program used to check the validity or syntactical correctness of a fragment of code or document. The term is commonly used in the context of validating HTML, CSS, and XML documents like RSS feeds, though it can be used for any defined format or language.

What is a validator class?

Defines a helper class that can be used to validate objects, properties, and methods when it is included in their associated ValidationAttribute attributes.


2 Answers

This guy puts them under app/validators/, which I've done as well, ever since I saw that blog post.

like image 194
fresskoma Avatar answered Sep 28 '22 11:09

fresskoma


Add this class in your lib directory and require it in your model and include it inside.

like image 35
shingara Avatar answered Sep 28 '22 13:09

shingara