extjs 4 - How change default message error in field - extjs

I have a form panel in http://jsfiddle.net/7CLWy/
here is my important code
items: [{
xtype: 'textfield',
fieldLabel: 'First Name',
allowBlank: false,
msgTarget: 'under',
name: 'firstName'
}, {
xtype: 'datefield',
allowBlank: false,
fieldLabel: 'Start date',
msgTarget: 'under'
}],
I want change default message error in field
How to change that. Thanks

The blankText property is the validation message when a field is required, and invalidText is the text when the field generically fails validation. You can add your own custom messages in these properties. Similarly, if you happened to be doing regex-based validation with the regex property, you could use the regexText field to provide a custom validation message.
items: [{
xtype: 'textfield',
fieldLabel: 'First Name',
allowBlank: false,
msgTarget: 'under',
name: 'firstName',
blankText: 'This should not be blank!'
}, {
xtype: 'datefield',
allowBlank: false,
fieldLabel: 'Start date',
msgTarget: 'under',
invalidText: 'This value is not a valid date!'
}, {
xtype: 'textfield',
fieldLabel: 'Digits followed by A,B,or C',
msgTarget: 'under',
name: 'someText',
regex: /^\d+[ABC]$/,
regexText: 'This must be a string of digits followed by A, B, or C!'
}]

msgTarget: 'side' will Add an error icon to the right of the field, displaying the message in a popup on hover only.
if you read the documentation carefully, one more option is there for msgTarget http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.Text-cfg-msgTarget
[element id] Add the error message directly to the innerHTML of the specified element. you have to add a "td" to the right side of the control dynamically with the id. then if you specify msgTarget: 'element id' it will work.
reference ->ExtJS4: How to show validation error message next to textbox, combobox etc

we can use validator method and return the custom message.
{
xtype: 'textfield',
fieldLabel: 'Digits followed by A,B,or C',
msgTarget: 'under',
name: 'someText',
validator : function(value){
var regex= /^\d+[ABC]$/;
if(!regex.test(value)){
return "'This must be a string of digits followed by A, B, or C!'"
}
return true;
}
}

To change the default active error message we use setActiveError( msg ).you see the following code.
items: [{
xtype: 'textfield',
fieldLabel: 'First Name',
allowBlank: false,
id:'fn',
msgTarget: 'under',
name: 'firstName',
}, {
xtype: 'datefield',
allowBlank: false,
fieldLabel: 'Start date',
msgTarget: 'under',
}
],
renderTo: Ext.getBody()
});
var m = Ext.getCmp('fn');
m.setActiveError("important"); //can change the text

Related

ExtJS Form UI get disrupt due to error message on field below

I am beginner to sencha architect and in ExtJS. When I placed error message in below textfield, this textfield moved little up because of vertical-align: middle has been assigned by sencha architect. How to fix that issue.
https://docs.sencha.com/extjs/6.0.2/guides/components/forms.html
Ext.create('Ext.form.Panel', {
renderTo: document.body,
title: 'User Form',
height: 350,
width: 300,
bodyPadding: 10,
defaultType: 'textfield',
items: [{
xtype: 'textfield',
flex: 1,
cls: [
'customInput',
'formTextBlack'
],
minHeight: 55,
fieldLabel: 'Pack Type',
labelAlign: 'top',
labelCls: 'blackLabelText14',
labelSeparator: ' ',
msgTarget: 'under',
name: 'packType',
fieldStyle: 'background:transparent',
readOnly: false,
allowBlank: false,
allowOnlyWhitespace: false
},
{
fieldLabel: 'Last Name',
name: 'lastName'
},
{
xtype: 'datefield',
fieldLabel: 'Date of Birth',
name: 'birthDate'
}
]
});
For this there is two way
Using height and scrollable:'y' or scrollable:true of form component. In this case scroll-able height of form will automatically increase/decrease when error message show/hide respectively. Please refer this working FIDDLE for this way.
Without height and scrollable of form
component. In this case height of form will
automatically increase/decrease when error message show/hide
respectively. Please refer this working
FIDDLE for this way.
I hope this will help you or guide you to achieve your requirement.
Code Snippet with height and scroll-able config
Ext.create('Ext.form.Panel', {
renderTo: document.body,
title: 'User Form',
height: 350,
scrollable: 'y',//true
width: '100%',
bodyPadding: 10,
defaults: {
flex: 1,
anchor: '100%',
labelSeparator: '',
margin: '10 20',
minHeight: 55,
xtype: 'textfield',
labelAlign: 'top',
allowBlank: false,
msgTarget: 'under' // location of the error message
},
items: [{
fieldLabel: 'Last Login Time',
name: 'loginTime',
regex: /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i,
maskRe: /[\d\s:amp]/i,
invalidText: 'Not a valid time. Must be in the format "12:34 PM".'
}, {
fieldLabel: 'First Name',
name: 'firstName'
}, {
fieldLabel: 'Middle Name',
allowBlank: true,
name: 'firstName'
}, {
fieldLabel: 'Last Name',
name: 'lastName'
}, {
fieldLabel: 'Email',
name: 'email'
}, {
xtype: 'datefield',
fieldLabel: 'Date of Join',
name: 'joinDate',
invalidText: '"{0}" not valid.enter value should be like "{1}" .' // custom error message text
}, {
xtype: 'datefield',
fieldLabel: 'Date of Birth',
name: 'birthDate',
invalidText: '"{0}" bad. "{1}" good.' // custom error message text
}]
});
Code Snippet without height and without scroll-able config
Ext.create('Ext.form.Panel', {
renderTo: document.body,
title: 'User Form',
width: '100%',
bodyPadding: 10,
defaults: {
flex: 1,
anchor: '100%',
labelSeparator: '',
margin: '10 20',
minHeight: 55,
xtype: 'textfield',
labelAlign: 'top',
allowBlank: false,
msgTarget: 'under' // location of the error message
},
items: [{
fieldLabel: 'Last Login Time',
name: 'loginTime',
regex: /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i,
maskRe: /[\d\s:amp]/i,
invalidText: 'Not a valid time. Must be in the format "12:34 PM".'
}, {
fieldLabel: 'First Name',
name: 'firstName'
}, {
fieldLabel: 'Middle Name',
allowBlank: true,
name: 'firstName'
}, {
fieldLabel: 'Last Name',
name: 'lastName'
}, {
fieldLabel: 'Email',
name: 'email'
}, {
xtype: 'datefield',
fieldLabel: 'Date of Join',
name: 'joinDate',
invalidText: '"{0}" not valid.enter value should be like "{1}" .' // custom error message text
}, {
xtype: 'datefield',
fieldLabel: 'Date of Birth',
name: 'birthDate',
invalidText: '"{0}" bad. "{1}" good.' // custom error message text
}]
});

How to get an element by index in ExtJS

I have a form with some elements in it. What I want to do is check if there is a specific element in position x. Is there a way so I can get the element x by its index value?
If you want to access the third textfield, you can use Ext.ComponentQuery
var thirdTextField = formcomponent.query('textfield')[2];
or using down
var thirdTextField = formcomponent.down('textfield:nth-child(3)'):
See https://fiddle.sencha.com/#fiddle/i2k
var form = Ext.create('Ext.form.Panel', {
title: 'Simple Form',
bodyPadding: 5,
width: 350,
layout: 'anchor',
defaults: {anchor: '100%'},
defaultType: 'textfield',
items: [{
fieldLabel: 'First Name',
name: 'first',
allowBlank: false
}, {
fieldLabel: 'Middle Name',
name: 'middle',
allowBlank: false
}, {
fieldLabel: 'Last Name',
name: 'last',
allowBlank: false
}],
renderTo: Ext.getBody()
});
form.down('textfield:nth-child(3)').setValue('Johnson')
}
//0-based index
var element = form.items.getAt(index);

Displayfield size from fieldLabel to value

hello i am have this component in form and i am want edit size from fieldLabel to value, i want do it size small.
{
xtype: 'displayfield',
value: 'ABC',
itemId: 'itemNumber',
fieldLabel: 'Item Number'
},
thanks)
Hi Try adding the following 'labelWidth' to your config one like the below example
{
xtype: 'displayfield',
value: 'ABC',
itemId: 'itemNumber',
fieldLabel: 'Item Number',
labelWidth: // Give a width that suits your req
}
Hope it helps you.

stop extjs TextField from accepting blanks and spaces

This is my textfield
siteNameField = new Ext.form.TextField({
id: 'siteNameField',
fieldLabel: 'Site Name',
maxLength: 50,
allowBlank: false,
anchor:'-15',
xtype:'textfield',
maskRe: /([a-zA-Z0-9\s]+)$/
});
As you can see it already has a check for blanks. But text field accepts space and I don't want that. I want no blank fields ... anything other than 'ONLY' spaces is acceptable .
Here is the FormPanel Code
voiceSiteCreateForm = new Ext.FormPanel({
labelAlign: 'top',
bodyStyle:'padding:5px',
width: 600,
items: [{
layout:'column',
border:false,
items:[{
columnWidth:0.5,
layout: 'form',
border:false,
//items: [siteNameField, siteNumberField,queueNameField,notifyFreqField,notifyStatusField]
items: [siteNameField, siteNumberField]
}]
}],
buttons: [{
text: 'Save and Close',
handler: createSite
},{
text: 'Cancel',
handler: function(){
// because of the global vars, we can only instantiate one window... so let's just hide it.
siteCreateWindow.hide();
}
}]
});
Please help,
Remove maskRe and use regex. Eg:
siteNameField = new Ext.form.TextField({
id: 'siteNameField',
fieldLabel: 'Site Name',
maxLength: 50,
allowBlank: false,
anchor:'-15',
xtype:'textfield',
regex: /[a-zA-Z0-9]+/
});

ExtJs Fieldset Collapsed issue

I have a fieldset with the option checkboxToggle:true. When the user checks/unchecks the checkbox, I don't want the fieldset to expand/collapse.
How can I accomplish this?
Thanks in Advance
You can implement the beforecollapse event on the fieldset like in the code below. But this way you won't be able to collapse the fieldset.
Setting collapsible: false doesn't seem to work.
xtype:'fieldset',
checkboxToggle:true,
title: 'User Information',
autoHeight:true,
defaults: {width: 210},
defaultType: 'textfield',
items :[{
fieldLabel: 'First Name',
name: 'first',
allowBlank:false
},{
fieldLabel: 'Last Name',
name: 'last'
},{
fieldLabel: 'Company',
name: 'company'
}, {
fieldLabel: 'Email',
name: 'email',
vtype:'email'
}
],
listeners: {
beforecollapse : function(p) {
return false;
}
}
or override Fieldset
Ext.override( Ext.form.FieldSet, {
onCheckChange: function(cmp, checked) {
if(this.collapsible) this.setExpanded(checked);
}
});

Resources