Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing custom validation before record created?

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.

like image 969
keruilin Avatar asked Jun 19 '10 23:06

keruilin


People also ask

How do you trigger a validation rule from flow?

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.

Do validation rules get enforced on any of the custom fields?

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.


2 Answers

In rails 3

before_validation_on_create :do_something

has been replaced with:

before_validation :do_something, :on => :create
like image 191
smek Avatar answered Sep 24 '22 06:09

smek


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
like image 32
John Bachir Avatar answered Sep 20 '22 06:09

John Bachir