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

136
Views
ExtJS - Optional model field with validation

In ExtJS 6.02 is it possible to have a field model that is optional but also has validation?

Example an email field that may or not be present but the email must be valid if it exists.

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

   identifier: {
      type      : 'sequential',
      seed      : 1,
      increment : 1
   },

   fields: [{
      name : 'date',
      type : 'date'
   }, {
      name : 'msg',
      type : 'string',
   }, {
      name : 'email',
      type : 'string',
   }],

   validators: {
      date: {
         type: 'presence'
      },
      msg: {
         type : 'length',
         min  : 2
      },
      email: {
         type : 'email'
      }
   }
});

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can override matcher of email validator to allow empty string:

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

    identifier: {
        type: 'sequential',
        seed: 1,
        increment: 1
    },

    fields: [{
        name: 'date',
        type: 'date'
    }, {
        name: 'msg',
        type: 'string',
    }, {
        name: 'email',
        type: 'string',
        allowBlank: true
    }],

    validators: {
        date: {
            type: 'presence'
        },
        msg: {
            type: 'length',
            min: 2
        },
        email: {
            type: 'email',
            // Override matcher to allow empty string
            matcher: /^$|^(")?(?:[^\."])(?:(?:[\.])?(?:[\w\-!#$%&'*+\/=?\^_`{|}~]))*\1@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/
        }
    }
});

Ext.application({
    name: 'Fiddle',

    launch: function () {
        var myModel = Ext.create('my_model', {
            date: new Date(),
            msg: 'Some Message',
            //email: 'mustermann@gmail.com',
            email: '',
        });
        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!