Validate only on update and not create

38 views
Skip to first unread message

Zachary Dixon

unread,
Jun 11, 2014, 12:48:28 PM6/11/14
to batm...@googlegroups.com
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

unread,
Jun 11, 2014, 12:50:45 PM6/11/14
to batm...@googlegroups.com
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

unread,
Jun 11, 2014, 12:56:53 PM6/11/14
to batm...@googlegroups.com
You could try conditional validation:  http://batmanjs.org/docs/api/batman.model_validations.html#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 batmanjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zachary Dixon

unread,
Jun 11, 2014, 2:04:33 PM6/11/14
to batm...@googlegroups.com, rdmo...@gmail.com
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

unread,
Jun 11, 2014, 3:25:17 PM6/11/14
to Zachary Dixon, batm...@googlegroups.com
Maybe related: if/unless validators were failing with multiple keys: https://github.com/batmanjs/batman/pull/1058

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
Forward
0 new messages