简体   繁体   English

Ext JS-验证文本字段

[英]Ext JS - Validating a TextField

I am a newbie to Ext JS so I need som ehelp in validating a text field. 我是Ext JS的新手,因此在验证文本字段时需要som ehelp。

Requirement is that user clicks on Add button and a new window shows up. 要求用户单击“添加”按钮,然后会出现一个新窗口。 This new windown will have two text fields ID and Name. 这个新窗口将具有两个文本字段ID和Name。 USer can give any input in Name fild. 用户可以在“名称”字段中输入任何内容。 But ID should not be the ID whihc is present in PLStore. 但是ID不应是PLStore中存在的ID。

I know I need to use validator but how to iterrate through PlStore value? 我知道我需要使用验证器,但是如何遍历PlStore值?

{xtype: 'form',
            bodyPadding: 10,
            title: '',
            items: [
                {
                    xtype: 'textfield',
                    store: 'PLStore',
                    anchor: '100%',
                    fieldLabel: 'ID'
                },
                {
                    xtype: 'textfield',
                    anchor: '100%',
                    fieldLabel: 'Name'
                },

PLStore contains following XML: PLStore包含以下XML:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets CachedTime="" DateCreated="2015-06-29T06:30:24" EndDate="2015-06-29T06:30:24" StartDate="2015-06-29T05:30:24" Version="15.0 SP4 Patch 4 (Jun 3, 2015)">
    <Rowset>

        <Row>
            <PLName>CA</PLName>
            <PLID>1001</PLID>
        </Row>
<Row>
            <PLName>VA</PLName>
            <PLID>1002</PLID>
        </Row>
<Row>
            <PLName>MH</PLName>
            <PLID>1003</PLID>
        </Row>

    </Rowset>
</Rowsets>

So if user puts ID as 1001, 1002 or 1003 - an error should pop up. 因此,如果用户将ID设置为1001、1002或1003,则会弹出错误。

How to go about it? 怎么做呢?

Thanks in advance ! 提前致谢 !

If you're using Ext > 4.0ish, you're on the right track with the validator property. 如果您使用的是Ext> 4.0ish,那么您将通过Validator属性进入正确的轨道。 You should be able to set this to a custom function, and do your validation within that. 您应该能够将此设置为自定义函数,并在其中进行验证。 In order to check against the store, one option would be to assign that store a storeId so that you can get a reference to it for validation purposes. 为了对商店进行检查,一种选择是为该商店分配一个storeId以便您可以获取对其的引用以进行验证。 So, your "id" field config might look something like: 因此,您的“ id”字段配置可能类似于:

{
    xtype: 'textfield',
    anchor: '100%',
    fieldLabel: 'ID',
    validator: function(fieldVal) {
        var ds = Ext.getStore('myStoreId'),
            ix = ds.find('id', fieldVal);
        return ix < 0;
    }
}

Where 'myStoreId' is the "storeId" you have assigned your data store, and "id" is assumed to be the id of the field we're looking for. 其中“ myStoreId”是您为数据存储分配的“ storeId”,而“ id”被假定为我们要查找的字段的ID。

It's been a little while since I've worked with the framework, so docs here in case I've missed something in the syntax: 自从我使用该框架以来已经有一段时间了,因此这里提供文档以防万一我错过了语法中的某些内容:

http://docs.sencha.com/extjs/4.2.3/#!/api http://docs.sencha.com/extjs/4.2.3/#!/api

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM