【问题标题】:ExtJS custom validator colorpickerExtJS 自定义验证器颜色选择器
【发布时间】:2016-03-21 21:21:58
【问题描述】:

我在 ExtJS 中有颜色选择器,用于选择颜色作为背景。但是我希望用户能够输入他们自己的十六进制代码。为了确保他们不会在字段中输入无效的 HEX 代码,我想验证他们的字符数量是否正确。

用户还可以输入名称颜色,例如:黑色、红色、metalbrightblue。

The SenchaDocumentation on validators was.. Less than helpful.

validator: function (val) {
    // remove non-numeric characters
    var tn = val.replace(/[^0-9]/g,''),
        errMsg = "Must be a 10 digit telephone number";
    // if the numeric value is not 10 digits return an error message
    return (tn.length === 10) ? true : errMsg;
}

一旦我尝试使用自定义验证器,我的颜色选择器就会消失。有人可以向我展示使用验证器的更全面的指南吗?谢谢。

编辑

我在 ExtJS 的 Selector 源代码上的扩展 .js 文件

Ext.define('name.name.colorpick.Selector', {
extend: 'Ext.ux.colorpick.Selector',
xtype: 'xtype-xtype-colorpick-selector',

editable:false,

getSliderAndAField: function() {
    return [];
},

getMapAndHexRGBFields: function(){
    var me = this;
    var result = this.callParent(arguments);
    var hexField = result.items[1].items[0];
    hexField.disabled = true;
    hexField.listeners = {
        change: function(field,value){
            me.setValue(value);
        }
    };

    return result;
},

getSliderAndHField: function (){
    var me = this;
    var result = this.callParent(arguments);
    var hField = result.items[1];
    hField.disabled = true;

    return result;
},

getSliderAndSField: function (){
    var me = this;
    var result = this.callParent(arguments);
    var sField = result.items[1];
    sField.disabled = true;

    return result;

},

getSliderAndVField: function (){
    var me = this;
    var result = this.callParent(arguments);
    var vField = result.items[1];
    vField.disabled = true;

    return result;

}

});

【问题讨论】:

  • 颜色选择器不允许输入自定义颜色,您是否为此使用任何自定义组件?
  • 不,我修改了一些源代码。我创建了第二个类,它扩展了原始类,并通过更改第二个类中的值,它基本上覆盖了源代码,改变了颜色选择器的功能。已经有允许人们输入自定义颜色的功能,它们只是被锁定了。
  • 如果您分享该扩展类的源代码将会很有帮助。
  • 刚刚添加了扩展。更改了一些名称,但应该清楚发生了什么。我基本上采用了源代码中已经存在的函数名称,并覆盖了文件中的现有变量。

标签: javascript validation extjs


【解决方案1】:

基于Validating css color names,您可以在此处为十六进制字段定义验证器。我假设您希望 hexField 是可编辑的。

getMapAndHexRGBFields: function (childViewModel) {
        var me = this;
        var result = this.callParent(arguments);
        var hexField = result.items[1].items[0];
        hexField.readOnly = false;
        hexField.validator=  function (val) {
            var validateColor = function(stringToTest) {
                if (stringToTest === "") { 
                    return false; 
                }
                if (stringToTest === "inherit") { return false; }
                if (stringToTest === "transparent") { return false; }

                var image = document.createElement("img");
                image.style.color = "rgb(0, 0, 0)";
                image.style.color = stringToTest;
                if (image.style.color !== "rgb(0, 0, 0)") { return true; }
                image.style.color = "rgb(255, 255, 255)";
                image.style.color = stringToTest;
                return image.style.color !== "rgb(255, 255, 255)";
            };

            var isValid = validateColor(val);

            var errMsg = "Not a valid Color";
            return isValid ? true : errMsg;
        };
        hexField.listeners = {
            change: function(field, value){
                if(field.isValid()) {
                    me.setValue(value);
                }
            }
        };
        return result;
    }

【讨论】:

  • 谢谢!这很有用。现在我只需要找到一种方法来禁用/启用“保存”按钮,具体取决于 HEX 颜色是否有效。
猜你喜欢
  • 2018-07-20
  • 2013-07-18
  • 2014-04-11
  • 2020-10-14
  • 2013-02-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 2017-04-21
相关资源
最近更新 更多