Make validators less painful to use #666

Open
tim-evans opened this Issue Dec 22, 2011 · 5 comments

4 participants

@tim-evans
SproutCore member

I'd like for validators to work well, and have documentation of how to use them. I've had to read through the source to even determine how to use them, and have had to cobble together a solution for triggering validation in an ad-hoc manner to make use of them. Some work here on making validators easy and painless to use would be welcome.

@mitchless

I've been using them a bit in my project. I can help out here.

@sevifives

What an appropriate issue number.

@hungle

I agree. If anyone have ideas on how to improve it, I can help out with the code changes.

@mitchless

@tim-evans Can you give me some examples of how you're using them ad-hoc?

@tim-evans
SproutCore member

@mitchless, sure!

We're using it for a login view, where each field gets validated on pre-submit. If a field is invalid, then we show a non-popup that points to the field that didn't validate correctly.

The form looks something like:

form: SC.View.design({

  validate: function () {
     if (this.get('username').validateSubmit() !== SC.VALDIATE_OK) {
       // do something
     } else if (this.get('password').validateSubmit() !== SC.VALDIATE_OK) {
       // do something
     } else {
       // fire submit on some target / action
     }
  },

  username: SC.TextFieldView.design({
    validator: SC.EmailValidator
  }),

  password: SC.TextFieldView.design({
    validator: SC.PasswordValidator,
    isPassword: YES
  }),

  submit: SC.ButtonView.design({
     targetBinding: SC.Binding.oneWay('.parentView'),
     action: 'validate'
  })
})

This is merely an approximation of what we have in our app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment