简体   繁体   English

Livecycle JavaScript…压缩if语句

[英]Livecycle javascript…condense if statement

I'm working on a form that will have a "Validate" button. 我正在处理一个带有“验证”按钮的表单。 The purpose of this button is to check and make sure all the fields are completed (this is what the project demands). 此按钮的目的是检查并确保所有字段均已填写(这是项目要求的)。 Below is the code that checks to see if the field is null and then changes the border color and displays a text box. 下面的代码检查该字段是否为null ,然后更改边框颜色并显示一个文本框。

if (form1.Main.sfRequestor.requestNameFirst.rawValue == null){
    form1.Main.sfRequest.txtValidate.presence = "visible";
    form1.Main.sfRequestor.requestNameFirst.border.edge.color.value = "255,0,0"
} else {
    form1.Main.sfRequest.txtValidate.presence = "hidden";
    form1.Main.sfRequestor.requestNameFirst.border.edge.color.value = "255,255,255"
};

if (form1.Main.sfRequestor.requestNameLast.rawValue == null){
    form1.Main.sfRequest.txtValidate.presence = "visible";
    form1.Main.sfRequestor.requestNameLast.border.edge.color.value = "255,0,0"
} else {
    form1.Main.sfRequest.txtValidate.presence = "hidden";
    form1.Main.sfRequestor.requestNameLast.border.edge.color.value = "255,255,255"
};

There are 20+ fields in several subforms that need to be checked. 有几个子表单中有20多个字段需要检查。 I'm trying to consolidate the code but am at a loss on how to do so. 我正在尝试合并代码,但如何操作却无所适从。 Can variables handle field names in Javascript? 变量可以处理Java语言中的字段名称吗?

You could easily make this into a simple function: 您可以轻松地将其变成一个简单的函数:

 function validateField(element) {
     if (element.rawValue == null) {
        form1.Main.sfRequest.txtValidate.presence = "visible";
        element.border.edge.color.calue = "255,0,0";
     }
     else {
        form1.Main.sfRequest.txtValidate.presence = "hidden";
        element.border.edge.color.calue = "255,255,255";      
     }
 }

And then just call it like so: 然后像这样调用它:

validateField(form1.Main.sfRequestor.requestNameFirst);
validateField(form1.Main.sfRequestor.requestNameLast);

To simplify even further, put all 20 elements in an array and loop 为了进一步简化,将所有20个元素放入数组并循环

var elements = [form1.Main.sfRequestor.requestNameFirst, form1.Main.sfRequestor.requestNameLast, ...];
elements.forEach(function(element) {
    validateField(element);
});

You can make this into a loop pretty easily, and could write it as an IIFE to keep your namespace clean 您可以很容易地使其进入循环,并可以将其编写为IIFE来保持名称空间整洁

(function (arr) {
    var txtValidate = form1.Main.sfRequest.txtValidate,
        i, e;
    for (i = 0; i < arr.length; ++i) {
        e = form1.Main.sfRequestor[arr[i]]; // cache me
        if (e.rawValue == null){
            txtValidate.presence = "visible";
            e.border.edge.color.value = "255,0,0"
        } else {
            txtValidate.presence = "hidden";
            e.border.edge.color.value = "255,255,255"
        }
    }
}(['requestNameFirst', 'requestNameLast']));

However, it looks like txtValidate.presence only ever gets set to whatever the last item's conditions were, are you sure you don't want to use a flag and set this last instead? 但是,看起来txtValidate.presence只能设置为最后一项的条件,您确定不希望使用标志并设置为最后吗? eg 例如

(function (arr) {
    var txtValidateState = 'hidden',
        i, e;
    for (i = 0; i < arr.length; ++i) {
        e = form1.Main.sfRequestor[arr[i]];
        if (e.rawValue == null){
            txtValidateState = "visible"; // any null makes txtValidate visible
            e.border.edge.color.value = "255,0,0"
        } else {
            e.border.edge.color.value = "255,255,255"
        }
    }
    form1.Main.sfRequest.txtValidate.presence = txtValidateState; // set last
}(['requestNameFirst', 'requestNameLast']));

Update for generic forms, assuming sfRequestor and sfRequest 假设sfRequestorsfRequest更新通用格式

(function (form, arr) {
    var txtValidateState = 'hidden',
        i, e;
    for (i = 0; i < arr.length; ++i) {
        e = form.sfRequestor[arr[i]];
        if (e.rawValue == null){
            txtValidateState = "visible";
            e.border.edge.color.value = "255,0,0";
        } else {
            e.border.edge.color.value = "255,255,255";
        }
    }
    form.sfRequest.txtValidate.presence = txtValidateState;
}(form1.Main, ['requestNameFirst', 'requestNameLast']));

Update Assuming sfRequest is a constant but sfRequestor could be something different 更新假设sfRequest是一个常量,但sfRequestor可能有所不同

(function () { // moved IIFE to protect namespace
    function validate(form, subform, arr) { // now named, new param subform
        var txtValidateState = 'hidden',
            i, e;
        for (i = 0; i < arr.length; ++i) {
            e = form[subform][arr[i]]; // select from subform
            if (e.rawValue == null){
                txtValidateState = "visible";
                e.border.edge.color.value = "255,0,0";
            } else {
                e.border.edge.color.value = "255,255,255";
            }
        }
        form.sfRequest.txtValidate.presence = txtValidateState; // assuming stays same
    }
    validate(form1.Main, 'sfRequestor', ['requestNameFirst', 'requestNameLast']);
    validate(form1.Main, 'sfClientInfo', ['firstname']);
    // if you have many here you can re-write as a loop again
}());

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

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