【问题标题】:Visualforce Apex:pageMessages validation rule IssueVisualforce Apex:pageMes​​sages 验证规则问题
【发布时间】:2015-03-09 12:49:36
【问题描述】:

我在 salesforce 中的自定义 employee__c 对象上定义了自定义验证规则。我使用带有自定义扩展的标准控制器 visualforce 页面向用户显示 UI 以进行数据输入。我的挑战是以易于阅读的方式向用户显示验证规则错误。这是我拥有的部分代码。

视觉力量

<apex:page standardController="Employee__c" extensions="EmployeeExtension" sidebar="false">
 <apex:sectionHeader ...
 <apex:form id=fr>
   <apex:pageMessages id="errMsg"/>
    <apex:pageBlock title="Employee Edit" mode="edit" >
    <apex:pageBlockButtons >
    <apex:commandButton action="{!mySave}" value="Save" reRender="errMsg"/>
    <apex:commandButton action="{!cancel}" value="Cancel"/>
    ....
 </apex:form>
</apex:page>

Apex 控制器

public class EmployeeExtension {
 ....
 public PageReference mySave(){
  ....
  try{
    upsert empList;
  } catch (DMLException ex) {
     ApexPages.addMessages(ex);
  }
 }
}

这显示了页面顶部的错误,这是我想要的方式,但它显示了两次。以下是它在页面顶部显示错误的方式。

  • error_message_from_custom_validation_comes_here
  • TriggerEmployee:第 0 行异常;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,error_message_from_custom_validation_comes_here

在我的整个控制器中,我没有任何其他 DML 操作,也没有在其他任何地方使用 ApexPages.addmessage。奇怪的是,如果我删除

ApexPages.addMessages(ex);

在保持 try catch 块不变的同时,我只看到

  • error_message_from_custom_validation_comes_here

我想知道为什么即使我没有从控制器发送任何内容,它也会在 vf 页面中显示页面消息。感谢您的所有回复,但我希望看到不涉及 javascript 或 jquery 的解决方案。

【问题讨论】:

    标签: salesforce visualforce apex


    【解决方案1】:

    你可以用这个:

    ApexPages.addMessage(
        new ApexPages.Message(
          ApexPages.severity.ERROR,
          ex.getMessage()
        )
      );
    

    或者你也可以模仿标准控制器:

    public class EmployeeExtension {
    ....
      public PageReference mySave(){
        /* do your thing */
        return this.standardController.save();
      }
    }
    

    这样你返回保存的页面引用,这是标准行为(如果你想模仿这个),如果有验证错误,它会显示在标签中,并且只会显示漂亮的格式化消息。

    保存时不需要重新渲染:

    <apex:page standardController="Employee__c" extensions="EmployeeExtension" sidebar="false">
      <apex:sectionHeader ...
      <apex:form id=fr>
        <apex:pageMessages/>
        <apex:pageBlock title="Employee Edit" mode="edit" >
          <apex:pageBlockButtons >
            <apex:commandButton action="{!mySave}" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        ....
      </apex:form>
    </apex:page>
    

    Salesforce/Visualforce 将为您完成剩下的工作 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多