#1096 ✓duplicate
bgentry

Issues with Contextual validations when some validations don't specify a context

Reported by bgentry | October 14th, 2009 @ 08:23 PM

On the validations page ( http://datamapper.org/docs/validations.html ) there is a section explaining contextual validations using the :when => [ :draft ] style.

There should also be at least one example usage of :unless => [ :draft ] to show the case in which you would skip validation only when a particular context is specified.

This does already work, I tested it, just should be added to the docs as it's a cool feature.

Comments and changes to this ticket

  • bgentry

    bgentry October 16th, 2009 @ 12:18 AM

    I take it back, this doesn't yet work. Would be useful to add :unless to contextual validations though.

    Also, we should consider letting validations run all the time if no context is specified.

    @@@ruby class User
    include DataMapper::Resource property :id, Serial property :email, String, :nullable => false property :first_name, String property :activated, Boolean

    validates_present :first_name, :when => [ :default ] validates_absent :activated, :when => [ :unactivated ] end

    
    In this example, the presence validation on :email will only run if the context is :default (or unspecified). If I run user.valid?(:unactivated), it will return true even if :email is blank. But it will fail to save due to the database-level protection.
    
  • bgentry

    bgentry October 16th, 2009 @ 12:20 AM

    Trying again...

    I take it back, this doesn't yet work. Would be useful to add :unless to contextual validations though.

    Also, we should consider letting validations run all the time if no context is specified.

    class User
      include DataMapper::Resource
      property :id, Serial
      property :email, String, :nullable => false
      property :first_name, String
      property :activated, Boolean
    
      validates_present :first_name, :when => [ :default ]
      validates_absent :activated, :when => [ :unactivated ]
    end
    

    In this example, the presence validation on :email will only run if the context is :default (or unspecified). If I run user.valid?(:unactivated), it will return true even if :email is blank. But it will fail to save due to the database-level protection.

  • bgentry

    bgentry October 16th, 2009 @ 12:22 AM

    • Tag changed from dm-validations, documentation, validation to bug, dm-validations, documentation, validation
    • Title changed from “Document using :unless for contextual validations” to “Issues with Contextual validations when some validations don't specify a context”
  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) October 16th, 2009 @ 11:53 AM

    • State changed from “new” to “duplicate”

    I think this is a duplicate of #1037

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages