En ExtJS 6.02, ¿es posible tener un modelo de campo que sea opcional pero que también tenga validación?
Por ejemplo, un campo de correo electrónico que puede o no estar presente, pero el correo electrónico debe ser válido si existe.
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' } } });Puede anular el comparador del validador de correo electrónico para permitir una cadena vacía:
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()); } });