backbone.validations icon indicating copy to clipboard operation
backbone.validations copied to clipboard

Conditional Validations

Open kanejoe opened this issue 13 years ago • 2 comments

Hi, thanks for the plugin - really useful.

I wonder if you knew how to implement conditional logic - the model has two attributes, company and person. On completing the form, validation should fail only if both are left empty.

would you have an idea how to structure this?

thanks

kanejoe avatar Feb 28 '12 17:02 kanejoe

+1

stoplion avatar Apr 09 '13 14:04 stoplion

Or you could do:

validate:
    keywords:
        custom: 'is_location_set'
    location:
        custom: 'is_keyword_set'

    is_location_set: (attr, value) =>
        if @get(attr) is '' and @get('location') is ''
            'no location'

    is_keyword_set: (attr, value) =>
        if @get(attr) is '' and @get('keywords') is ''
            'no keywords'

stoplion avatar Apr 09 '13 16:04 stoplion