Is it possible to create my own validations within the Rails model
validation framework?
In particular I’m interesting in being able to create a global (i.e. not
associated with a particular field) business logic validation
exception(s)
to pass back to the controller. They could then be listed on the View
as an
exception error, but not associated with a particular field.
I’m thinking for higher level business logic rule exceptions like this
it
may be better doing it the above way as opposed to creating an exception
(
e.g. businessLogic exception class) - but any ideas/comments welcome on
this.
You can add any validation logic you like to the validate method on
any active model class. It can validate any combination of attributes
of the object.
If you are wanting to add new meta-programming methods like
validate_presence_of that takes some additional digging I have not
done yet.
How does this look (I’m not at my rails PC at the moment)? i.e.
regarding
the use of Global (not attribute specific) validations within Model
class Person < ActiveRecord::Base
protected
def validate_on_create
if !complex_business_logic_passes?
errors.add_to_base(“complex business logic failed”)
end
end
def complex_business_logic_passes?
…
return result # true if passes
end
end
Business Logic Errors = <%= error_message_on "object", "base" %>
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.