Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

266 mass apply rules #337

Merged
merged 6 commits into from Nov 8, 2013
Merged

266 mass apply rules #337

merged 6 commits into from Nov 8, 2013

Conversation

stevegreatrex
Copy link
Contributor

The is a suggested implementation of setRules that will let you set validations on an entire object graph from a single definition.

var rules = {
    //will apply required, min & max to viewModel.property
    property: {
        required: true,
        min: 10,
        max: 99,
    },

    //will apply required to viewModel.property2.childProperty
    property2: {
        childProperty: { required: true }
    },

    //will apply email to the elementProperty of each item in
    //viewModel.arrayProperty
    arrayProperty: {
        elementProperty: { email: true }
    }
};

var viewModel = {
    property: ko.observable(),
    property2: {
        childProperty: ko.observable()
    },
    arrayProperty: ko.observableArray([
        { elementProperty: ko.observable() },
        { elementProperty: ko.observable() }
    ])
};

ko.validation.setRules(target, rules);    

Eventually this could be used to pull validation definitions from models, from ASP.NET DataAnnotations, or from any other source.

Any comments/changes/improvements?

stevegreatrex added a commit that referenced this pull request Nov 8, 2013
@stevegreatrex stevegreatrex merged commit c41785f into master Nov 8, 2013
@stevegreatrex
Copy link
Contributor Author

Fixes #266

@pitus
Copy link

pitus commented Aug 21, 2015

If any rules are applied to an observableArray property, there should be an option to re-apply them if the array changes, otherwise only the initial array's set of values will be validated. Perhaps subscribe to the observableArray property and re-apply the rule set to added values ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants