ExtJs之ExtJs.Model验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExtJs之ExtJs.Model验证相关的知识,希望对你有一定的参考价值。

没有搞好,但知道意思和配置,并且好像4和3的没有兼容性语法,所以网上找了新语法才出来了点东东。

<!DOCTYPE html>
<html>
<head>
    <title>ExtJs</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" type="text/css" href="ExtJs/packages/ext-theme-crisp/build/resources/ext-theme-crisp-all.css">
		<script type="text/javascript" src="ExtJs/ext-all.js"></script>
		<script type="text/javascript" src="ExtJs/bootstrap.js"></script>
		<script type="text/javascript" src="ExtJs/packages/ext-theme-crisp/build/ext-theme-crisp.js"></script>

    <script type="text/javascript">
			Ext.onReady(function(){
        //Ext.regModel(‘ProductInfo‘, {
        //  fields: [‘productName‘, ‘productNum‘, ‘productPrice‘]
        //});
        /*
        Ext.data.validations.presenceMessage = ‘必须是有效值‘;
        Ext.data.validations.lengthMessage = ‘长度错误‘;
        Ext.data.validations.formatMessage = ‘格式错误‘;

        Ext.apply(Ext.data.validations, {
          number: function(config, value){
            if(value === undefined){
              return false;
            }
            var min = config.min,
                max = config.max;
            if((min && value < min) || (max && value > max)) {
              return false;
            }else {
              return true;
            }
          },
          numberMessage: ‘数据范围错误‘
        })
        */
        Ext.define(‘Ext.data.validator.Number‘, {
          extend: ‘Ext.data.validator.validator‘,
          alias: ‘data.validator.number‘,
          type: ‘number‘,
          config: {
            min: 160
          },
          validate: function(value){
            console.log(value);
            var result = Ext.isNumber(value);
            if(result){
              result = value > 0 && value < this.getMax();
            }
            return result ? result : this.getMessage();
          }
        });

        Ext.define(‘User‘, {
            extend: ‘Ext.data.Model‘,
            fields: [
              {name: ‘name‘, type: ‘string‘},
              {name: ‘age‘, type: ‘int‘},
              {name: ‘phone‘, type: ‘string‘}
            ],
            validators: {
              age: ‘presence‘,
              //age: {type: ‘number‘, min: 30},
              name: {type: ‘length‘, min: 5}
            }
        });

        var user = Ext.create(‘User‘, {
          name: ‘tom‘,
          age: 24,
          phone: ‘555-666-6666‘
        });
        var errors = user.getValidation();
        /*
        var message = [];
        errors.each(function(v){
          message.push(v.field + ‘: ‘ + v.message)
        });

        alert(message.join(‘\n‘));
        */
        alert(errors);
        alert(user.get(‘name‘));


      });
    </script>
</head>
<body>
<div id=‘tpl-table1‘></div>
<br>
<div id=‘tpl-table2‘></div>
</body>
</html>

  

以上是关于ExtJs之ExtJs.Model验证的主要内容,如果未能解决你的问题,请参考以下文章

用于嵌套JSON结构的Extjs Model

即使服务器出错,Sencha extjs model.erase 也会删除模型

ExtJS - 如何使用代理,模型?它们有啥关系?

Extjs3.2 RowEditor 字段验证

extjs常用技巧

Extjs GridPanel 验证