Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS4: How to show validation error message next to textbox, combobox etc

Tags:

extjs

extjs4

I need to implement validation messages that appear right next to invalid field. Any help would be appreciated.

like image 457
berliner Avatar asked Jun 03 '12 13:06

berliner


People also ask

How can I see all validation errors in one place?

Add a ValidationSummary control to the page at the location where you want to display the collected error messages. Set the ErrorMessage and Display properties of the individual validation controls. (Default) Each error message appears as a bulleted item. Each error message appears on its own line.

What is validation error message?

The below validation errors are the most common and will respond with some details, including a list of validation messages. Validation errors typically occur when a request is malformed -- usually because a field has not been given the correct value type, or the JSON is misformatted.


5 Answers

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.

like image 141
Jom Avatar answered Oct 19 '22 08:10

Jom


The msgTarget: 'elementId' can work, but it seem very limited, particularly when you want multiple instances of one reusable ExtJs component (and therefor multiple instances of the same msgTarget element). For example I have an MDI style editor where you can open multiple editors of one type in a tab interface. It also doesn't seem to work with itemId or recognize DOM/container hierarchy.

I therefor prefer to turn off the default handling if I don't want exactly one of the built in message display options by setting msgTarget: none and then performing my own message display by handling the fielderrorchange event which is designed for exactly this scenario. In this case I can now respect hierarchy of my forms even with multiple instances of the same editor form as I can select the error display element relative to the editor.

Here's how I do it:

{
    xtype: 'fieldcontainer',
    fieldLabel: 'My Field Label',
    layout: 'hbox',   // this will be container with 2 elements: the editor & the error
    items: [{
        xtype: 'numberfield',
        itemId: 'myDataFieldName',
        name: 'myDataFieldName',
        width: 150,
        msgTarget: 'none',  // don't use the default built in error message display
        validator: function (value) {
            return 'This is my custom validation message. All real validation logic removed for example clarity.';
        }
    }, {
        xtype: 'label',
        itemId: 'errorBox', // This ID lets me find the display element relative to the editor above
        cls: 'errorBox'     // This class lets me customize the appearance of the error element in CSS
    }],
    listeners: {
        'fielderrorchange': function (container, field, error, eOpts) {
            var errUI = container.down('#errorBox');
            if (error) {
                // show error element, don't esape any HTML formatting provided
                errUI.setText(error, false);
                errUI.show();
            } else {
                // hide error element
                errUI.hide();
            }
        }
    }
}
like image 35
BenSwayne Avatar answered Oct 19 '22 07:10

BenSwayne


See the msgTarget config of the control. msgTarget: 'side' would put the validation message to the right of the control.

like image 35
JohnnyHK Avatar answered Oct 19 '22 08:10

JohnnyHK


Use msgTarget 'side' for validation in right side and msgTarget 'under' for bottom

     items: [{
                xtype: 'textfield',
                fieldLabel: 'Name',
                allowBlank: false,
                name: 'name',
                msgTarget: 'side',
                blankText: 'This should not be blank!'
            }]
like image 2
php Avatar answered Oct 19 '22 08:10

php


You can use 'msgTarget: [element id]'. You have to write html in order to use element id instead of itemId. The validation function adds a list element under an element that you set as 'msgTarget'. In case you want to show elements that you want for the validation, you can pass html instead of just a text.

{
    xtype: 'container',
    items: [
        {
            xtype: 'textfield',
            allowBlank: false,
            msgTarget: 'hoge'
            blankText: '<div style="color:red;">validation message</div>', // optional
        },
        {
            xtype: 'box',
            html: '<div id="hoge"></div>' // this id has to be same as msgTarget
        }
    ]
}
like image 2
Kohei Mikami Avatar answered Oct 19 '22 09:10

Kohei Mikami