Salesforce:创建有条件要求的Apex表单字段

时间:2012-04-17 17:00:16

标签: jquery salesforce visualforce

我正在使用Salesforce中的自定义表单,并且如果选中某个复选框,则可选字段会动态变为必填字段。

例如,如果用户选中电子转帐选项进行付款,则应启用与 EFT 相关的所有字段,并且Apex应根据需要显示这些字段字段(在非可选项中)。

我目前正在使用jQuery启用这些字段,但我很难找到如何将字段更改为Apex必需字段的示例。

感谢您解决此问题的任何帮助或线索。

1 个答案:

答案 0 :(得分:0)

Validation Rules会为你效劳吗?对于考虑了复选框的每个动态必需字段,您可以有一个验证规则。这将负责服务器端验证,但如果您使用Apex Classes and Extensions(如JavaScript或jQuery)以外的任何其他内容来提交数据,则需要单独处理客户端。

要利用Visual Force提供的默认CSS样式,如果用户尝试保存而不输入值,则将“error”类添加到input元素。

以下是我将如何处理客户端验证的代码片段(在noConflict模式下使用jQuery作为j$):

// check required fields
j$(RequiredFields).each(function(index,field)
{
    if(!(field.val() && field.val() != "" && field.val() != "--None--")) { field.addClass("error"); RequiredFieldsInError.push(field); } 
    else { field.removeClass("error"); }
});

if(RequiredFieldsInError.length > 0)
{
    var RequiredFieldLabels = [];

    j$(RequiredFieldsInError).each(function(index,field){
        var RequiredFieldLabel = j$("label[for$="+j$(field).attr('id')+"]").html().trim(); RequiredFieldLabels.push(RequiredFieldLabel);
        field.after("<div id='"+field.attr("id")+"_error' class='errorMsg' >"+RequiredFieldLabel+" is Required.</div>");
    });

    if (RequiredFieldLabels.length > 1) {
        RequiredFieldLabels[RequiredFieldLabels.length - 1] = "and " + RequiredFieldLabels[RequiredFieldLabels.length - 1];
    }

    j$("#ErrorSummary")
        .html(RequiredFieldLabels.join(", ")+" "+(RequiredFieldLabels.length > 1 ? "are" : "is a")+" required field(s). ")
        .css("color","#CC0000");

    return;
}