I want to execute a custom validation before the record is created?
It looks as if this is the right method: before_validation_on_create. For example:
before_validation_on_create :custom_validation
But am not sure. Any help would be appreciated.
Validation rules are specific for the objects. In order to create a validation rule, you have to navigate to the object in the Object Manager, click on Validation Rules and create a new one. It has a simple interface that lets you select fields from the current record or from the parent record/object.
The detail page of a custom activity field doesn't list associated validation rules. Workflow rules and some processes can invalidate previously valid fields. Invalidation occurs because updates to records based on workflow rules and also on process scheduled actions don't trigger validation rules.
In rails 3
before_validation_on_create :do_something
has been replaced with:
before_validation :do_something, :on => :create
before_validation_on_create hooks happen before validation on create… but they aren't validations themselves.
What you probably want to do is use validate
and a private method which adds to the error array. like this:
class IceCreamCone
validate :ensure_ice_cream_is_not_melted, :before => :create
private
def ensure_ice_cream_is_not_melted
if ice_cream.melted?
errors.add(:ice_cream, 'is melted.')
end
end
end
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