Skip to content

Latest commit

 

History

History
79 lines (62 loc) · 2.31 KB

README.md

File metadata and controls

79 lines (62 loc) · 2.31 KB

{}[http://travis-ci.org/[leveille]/[spine.validate]]

Simple model validation framework for Spine. Allowing you to mix strong validation rules with fluid english context.

Install

Download spine.validation.js and include it in your html.

<script type="text/javascript" src="spine.validate.js"></script>

Usage

You must first start with a spine model

    var model = Spine.Model.setup("Person", ["first","last","age","birth","address1","city","state","zip"]);

Then include the validation object

    model.include(Spine.Validate);

Then setup some super awesome rules to go along with that

    model.include({
        rules: function(check) { return [
            RuleFor("first")
                .WhenNot().OnCreate()
                .ItShouldBe()
                .Required(),

            RuleFor("first")
                .It()
                .Must(function(field,record) {
                    return field === record.last;
                })
                .When(function(record) {
                    return record.last === "Palmer";
                })
                .Message("first and last names must match"),

            RuleFor("last")
                .ItIs()
                .Required()
                .And().Also()
                .Matches(/[A-Z]+/i),

            RuleFor("age")
                .ItShouldBe()
                .Between(18,25),

            RuleFor("birth")
                .It()
                .IsInPast(),

            RuleFor("state")
                .ItIs()
                .Required()
                .When(function(record) {
                    return record.zip !== undefined && record.zip.length > 0
                })
                .And().MaxLength(2),

            RuleFor("zip")
                .WhenNot().OnCreate()
                .It().IsNumeric()
                .Also().ItIs().Length(5),

            RuleFor("email")
                .ItIs()
                .EmailAddress()
        ]}
    });

After that whenever spine would normally call your custom "validate" method it will run through all the rules and send out error events just like you are used too. If you aren't familiar with this process see the spine documentation.