Skip to content

Loading…

custom validation suggestion for README #445

Open
ccmcbeck opened this Issue · 2 comments

1 participant

@ccmcbeck

When I was evaluating this gem, I wondered if I could use it for all my client_side validations. I've seen threads on Railscasts, the simple_form Google Group, etc where people wonder the same thing.

We all want the goodness of c_s_v, but nothing is worse than having multiple validation engines dueling away in the UI (so I'm glad to see you are working on a backbone.js plugin).

To allay these fears, it would be nice if the README documented a few more custom validation use cases. Forex, I wanted to see a pattern for validations that depend on multiple attributes to fire when either field changes. I extracted my solution from: https://github.com/bcardarella/client_side_validations/wiki/Shared-validations-for-multiple-fields

$('#last_name, #company')
      .change(-> $('#first_name').change(); true)
      .focusout(-> $('#first_name').focusout(); true)

My validation decorates #first_name with the error, but now it fires when the other fields are modified or visited. This may seem obvious to some, but it took me a while to figure it out. I think if more people knew up-front (on the README) how comprehensive this gem was for all your validation needs, they would dive in faster. I wish I had done it sooner.

Just my 2 cents.

@ccmcbeck

Perhaps the README could link to a Custom Validations wiki page like you did for the Plug Ins and the community could add some examples like the multiple_fields example above and the one I proposed.

@ccmcbeck

This approach also works very nice with backbone.js

class MyApp.Views.Person extends Backbone.View

  el: '#form_person'

  events:
    'change #person_last_name'    : -> $('#person_first_name').change(); true
    'change #person_company'      : -> $('#person_first_name').change(); true
    'focusout #person_last_name'  : -> $('#person_first_name').focusout(); true
    'focusout #person_company'    : -> $('#person_first_name').focusout(); true

Since this was coffee script (which is opinionated about return values), I needed to return true to continue event bubbling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.