Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
ExtJS - Model field with mixed value types

In ExtJS 6.2, how can I define a model field which can be of different values, for example, string or boolean and validate that it can either be true, false or a string?

In the bellow model the value can be either a string or boolean.

Ext.define('my_model', {
   extend  : 'Ext.data.Model',

   fields: [{
      name   : 'name',
      type   : 'string',
      unique : true
   }, {
      name : 'value',
      convert(value) {
         switch (value) {
            case 'Y': return true;
            case 'N': return false;
            default : return value;
         }
      }
   }],

   validators: {
      value: {
         // How do I validate it?
      }
   }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use auto field and validate property:

Ext.define('my_model', {
    extend: 'Ext.data.Model',

    fields: [{
        name: 'name',
        type: 'string',
        unique: true,
    }, {
        name: 'value',
        type: 'auto',
        convert(value) {
            switch (value) {
            case 'Y':
                return true;
            case 'N':
                return false;
            default:
                return value;
            }
        },
        validate: function (value) {
            return ['boolean', 'string'].indexOf(typeof value) !== -1;
        }
    }]
});

Ext.application({
    name: 'Fiddle',

    launch: function () {
        var myModel = Ext.create('my_model', {
            name: 'Some name',
            value: 'N'
        });
        console.log(myModel.getData());
        console.log(myModel.isValid());
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!