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
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.
Defines a helper class that can be used to validate objects, properties, and methods when it is included in their associated ValidationAttribute attributes.
This guy puts them under app/validators/
, which I've done as well, ever since I saw that blog post.
Add this class in your lib directory and require it in your model and include it inside.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With