Skip to content

Latest commit

 

History

History
154 lines (112 loc) · 2.99 KB

xone_validate.md

File metadata and controls

154 lines (112 loc) · 2.99 KB

Model Validation

var Record = MODEL.register('Record', (function(){

    function RecordClass(data){

        this.id = data.id;
        this.name = data.name;
    }
    
    /* Define Model Validation */

    RecordClass.prototype.validate = function(){
        
        return this.id >= 0;
    };
    
    return RecordClass;
})());

Once a validation has been defined to its model, each write process of a model executes the validation and skip persistent modifications if an invalidation occurs. You are also able to perform validations directly:

var record = MODEL.Record.create({

    id: -1,
    name: 'foobar'
});

console.log(record.validate()); // -> ["Model Validation failed"]
console.log(record.isValid()); // -> false

Note: Calling .validate() returns an array of error strings. If the returned array is empty all validations perform successfully. You may prefer using the model helper .isValid() which returns a boolean.

Multiple Validations

    
    /* Define Model Validation */

    RecordClass.prototype.validate = {
    
        'Record has valid ID': function(){
        
            return this.id >= 0;
        },
        
        'Record has valid Name': function(){
                
            return this.name.length ? true : false;
        }
    };

Example:

var record = MODEL.Record.create({

    id: -1,
    name: ""
});

console.log(record.validate()); // -> ["Record has valid ID", "Record has valid Name"]
console.log(record.isValid()); // -> false

Form Validation

Built-in Types:

  • integer, int
  • numeric, num
  • text, txt
  • float, decimal, dec
  • alphanumeric, anum
  • filename, file
  • url
  • email

Additional Options:

  • min
  • max
  • charset
  • regex

Examples

<input class="form-validate"
       type="number"
       value="35"
       data-validate-type="integer"
       data-validate-min="1"
       data-validate-max="99"> <!-- min/max: numeric value -->
<input class="form-validate" 
       type="number" 
       value="3.5"
       data-validate-type="float" 
       data-validate-min="-9.99"
       data-validate-max="+9.99">
<input class="form-validate" 
       type="text" 
       value="A25i9"
       data-validate-type="alphanumeric"
       data-validate-min="5"
       data-validate-max="5"> <!-- min/max: text length -->
<input class="form-validate" 
       type="text" 
       value="ABabAc"
       data-validate-charset="ABCabc">
<input class="form-validate" 
       type="text" 
       value="XONE"
       data-validate-regex="[A-Z]">

Note: Custom regex definitions from above will be performed as new Regex(/[A-Z]/, 'g') internally.

Register Custom Validation Types

Define Validation:

APP.VALIDATE['whitespace'] = function(value){
    
    return value.indexOf(' ') === -1;
};

Usage:

<input class="form-validate" 
       type="text" 
       value="x_o_n_e"
       data-validate-type="whitespace">