4

What is presence type validation?

validations: [{
            type: 'presence',  
            field: 'age'
            }]  

Please help for illustrative example.

Peter Ivan
  • 1,467
  • 2
  • 14
  • 27
Michael Phelps
  • 3,451
  • 7
  • 36
  • 64

1 Answers1

6

According to documentation,It simply ensures that the field has a value.

First define a Employee model like below.

Ext.define('App.model.Employee', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            { name: 'id', type: 'int' },
            { name: 'name', type: 'string' },
            { name: 'salary', type: 'float' },
            { name: 'address', type: 'string' },
        ],
        validations: [
            { type: 'presence', field: 'name' }
        ]
    }
});

Now create an instance of Employee model previously created. Note that we are not assigning value to name field,which has presence validation.

var newEmployee = Ext.create('App.model.Employee', {
    id: 1,
    salary: 5555.00,
    address: 'Noida'
});

// Validating a model.
var errors = newEmployee.validate();
//Now print the error message
errors.each(function (item, index, length) {
    console.log('Field "' + item.getField() + '" ' + item.getMessage());//Field "name" must be present
});
dReAmEr
  • 6,986
  • 7
  • 36
  • 63