5

How I can I avoid having validation for update while having it for create? For instance:

I wish to have an image field to have presence validation on create. But want to avoid it in edit and assume the previous value in case of no change.

Note: I am using Padrino.

Jikku Jose
  • 18,306
  • 11
  • 41
  • 61

3 Answers3

10

In Sequel, validations are generally done at the instance level using the validation_helpers plugin. So you just use a standard ruby conditional if you only want to validate it for new objects and not for existing ones:

plugin :validation_helpers
def validate
  super
  validates_presence :image if new?
end
Jeremy Evans
  • 11,959
  • 27
  • 26
0

you can say something like:

validates :image, :presence => true, on: :create

that means you will do validation only when do create.

Marko Krstic
  • 1,417
  • 1
  • 11
  • 13
0

In Rails,

The "validates" keyword in Rails takes a parameter that allows you to do this:

validates :your_validator, :presence => true, :on => :create

You can also pass an array there:

validates :your_validator, :presence => true, :on => [:create, :update]

In Padrino,

I believe padrino using datamapper - DM has contextual validations. So, you could do a contextual validation.

validates :image, :presence => true, :when => [:custom]

Then while saving, you can do this:

post.save(:custom)
# OR
post.valid?(:custom)

The context :custom can be used in different places.

sandeep
  • 521
  • 2
  • 7