Validate only on update and not create

Skip to first unread message

Zachary Dixon

Jun 11, 2014, 12:48:28 PM6/11/14
I have a model where a few attributes are required only after the record has been created. It's created in the backend and and the client has no way of creating them. Is there a way to create this validation since presence: true seems like it validates on any kind of save.

Zachary Dixon

Jun 11, 2014, 12:50:45 PM6/11/14
I should also add that I need to create these on the demo version which uses LocalStorage, otherwise I won't have the problem on the production app that uses a Rails API

Robert Mosolgo

Jun 11, 2014, 12:56:53 PM6/11/14
You could try conditional validation:

For example,

@validate "name", presence: true, if: "id"

where presence of "id" shows that it has already been created. You could define another accessor and use that instead of "id" if you need a more robust test. 

In the long run, it would be nice to imitate Rails' `validates ... on: ` in batman.js!

You received this message because you are subscribed to the Google Groups "batman.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Zachary Dixon

Jun 11, 2014, 2:04:33 PM6/11/14
Yeah the @validate on: would definitely be nice.

I tried:
@validate 'street_address_1', 'city', 'state', 'zip_code', if: "id", presence: true

but it doesn't seem to be working, even in the callback on the create I'm seeing 4 errors and the record.get('id') returns undefined.

Robert Mosolgo

Jun 11, 2014, 3:25:17 PM6/11/14
to Zachary Dixon,
Maybe related: if/unless validators were failing with multiple keys:

You could try splitting them up to separate `@validate` calls to work around that bug, or just use a full-on custom validation:

Reply all
Reply to author
0 new messages