Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (67 sloc) 2.67 KB

NOTICE: This project is no longer maintained. If you would like commit access to the repository please open up an issue. If you are still using this in production I would like to hear from you as well. It was fun putting this together but we eventually moved to different javascript framework.

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(RuleFor) { 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.

Contributors

  • Nathan Palmer (author)
  • Aaron Hansen
  • Jason Leveille