Search code examples
validationsproutcore

SproutCore field binding and validation


I'm new to SproutCore and have hooked my model and view together via the valueBinding property of the view fields.

However I'm not sure how to hook up SC.Validators to the view so that data is validated before being copied into the model. I've looked at the SproutCore API and although it talks about the "validate" attribute of the tag, I'm not sure how to set that in my SC code.

A code example would be appreciated if anyone knows how to do this.


Solution

  • Here is an example, with the NotEmpty validator:

    field: SC.TextFieldView.design( { 
        layout:{top:44, height:24, width:150, centerX:10}, 
        contentBinding:this.get('contentPath'), 
        contentValueKey:'name', 
        validator: SC.Validator.NotEmpty.extend({ }) 
    })