我需要实现验证消息,出现在无效字段旁边。希望得到帮助。
我需要实现验证消息,出现在无效字段旁边。希望得到帮助。
msgTarget: 'side'
表示将在字段右侧添加一个错误图标,在悬停时以弹出窗口显示消息。
如果您仔细阅读文档,还有一个选项可用于 msgTargethttp://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.Text-cfg-msgTarget
[element id] 将错误消息直接添加到指定元素的 innerHTML 中。
您必须动态地向控件右侧添加一个带有 id 的 "td"。然后,如果指定 msgTarget: 'element id'
,它将起作用。
msgTarget: 'elementId'
可以使用,但似乎非常有限,特别是当您想要一个可重用的 ExtJs 组件的多个实例(因此具有相同的 msgTarget 元素的多个实例)时。例如,我有一个 MDI 样式的编辑器,您可以在选项卡界面中打开一个类型的多个编辑器。它也似乎不能与 itemId
一起使用或识别 DOM/容器层次结构。msgTarget: none
来关闭默认处理,然后通过处理 fielderrorchange
事件来执行自己的消息显示,该事件专为此场景设计。在这种情况下,我现在可以尊重我的表单层次结构,即使有多个相同的编辑器表单实例,因为我可以选择相对于编辑器的错误显示元素。{
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();
}
}
}
}
请查看控件的msgTarget配置。将msgTarget: 'side'
设置为控件右侧将显示验证消息。
在右侧进行验证时,请使用msgTarget 'side',在底部进行验证时,请使用msgTarget 'under'
items: [{
xtype: 'textfield',
fieldLabel: 'Name',
allowBlank: false,
name: 'name',
msgTarget: 'side',
blankText: 'This should not be blank!'
}]
{
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
}
]
}