PDA

View Full Version : custom validator different messages for different test



harrypottar
1 Sep 2020, 2:21 PM
I have written a couple of custom validators, they are basically working.

However, with confirmInstancePath I have too test.

The value must start with another value. (working) and it must be longer that the other value indicating that it's been added too (working)

However I would like to give different error messages for each failure

confirmInstancePathText: 'Instance Root Path must start with the sites master path',

or

confirmInstancePathText: 'Please add directory path to the sites master path',



Ext.define('overrides.form.field.VTypes', {
override: 'Ext.form.field.VTypes',

confirmInstancePath: function(val, field) {
if (field.siteInfoField) {
var record = field.up('form').down('#' + field.siteInfoField).getSelectedRecord(),
rootPath = record.get('master_root_path');


return ( val.startsWith(rootPath) && (val.length > rootPath.length ) );
}
return true;
},
confirmInstancePathText: 'Instance Root Path must start with the sites master path',


confirmPassword: function(val, field) {
if (field.initialPassField) {
var pwd = field.up('form').down('#' + field.initialPassField);
return (val == pwd.getValue());
}
return true;
},


confirmPasswordText: 'Passwords do not match'
});


TIA
Harry

harrypottar
2 Sep 2020, 8:49 AM
Figured it out, can set the corresponding error message text in the method



Ext.define('overrides.form.field.VTypes', {
override: 'Ext.form.field.VTypes',

confirmInstancePath: function(val, field) {
if (field.siteInfoField) {
var record = field.up('form').down('#' + field.siteInfoField).getSelectedRecord(),
rootPath = record.get('master_root_path');

if ( !val.startsWith(rootPath) ) {
this.confirmInstancePathText = 'Instance Root Path must start with the sites master path';
return false;
}
if ( val.length <= rootPath.length ) {
this.confirmInstancePathText = 'Please add directory path to the sites master path';
return false;
}
}
return true;
},
confirmInstancePathText: 'This field is required',


confirmPassword: function(val, field) {
if (field.initialPassField) {
var pwd = field.up('form').down('#' + field.initialPassField);
return (val == pwd.getValue());
}
return true;
},


confirmPasswordText: 'Passwords do not match'
});