对具有onChange脚本的字段进行xpages验证

时间:2014-07-16 10:50:26

标签: xpages xpages-ssjs

有一个required字段:

 <xp:this.validators>
                            <xp:validateRequired
                                message="Required field. Please add some text.">
                            </xp:validateRequired>
 </xp:this.validators>

此外,此字段的值将(使用onChange事件)复制到其他字段:

     <xp:eventHandler event="onchange" submit="true"refreshMode="norefresh">
          <xp:this.action><![CDATA[#{javascript:Cdoc.setValue("dlg_Localitate",Cdoc.getValue("txt_LocalitateCompanie"));
        Cdoc.setValue("dlg_Localitate_1",Cdoc.getValue("txt_LocalitateCompanie"))}]]>                                                                                   
         </xp:this.action>
   </xp:eventHandler>

当我单击该字段以填充它时,会出现一个不方便的问题:出现验证消息。是因为该字段最初是空的,我添加的代码是在onChange事件中吗?

我希望在用户保存文档之前根据需要使用此字段。

我尝试通过CSJS设置值,但没有结果......

var string = XSP.getElementById("#{id:inputText1}").value
XSP.getElementById("#{id:txt_LocalitateS}").value = string
XSP.getElementById("#{id:txt_LocalitateP}").value = string

另外,假设我为inputText1输入一个值,稍后我输入一个新值...如何使用新值自动更新其他两个字段?

我试过这样的事情:

<xp:inputText id="inputText1" value="#{Cdoc.txt_LocalitateCompanie}"
                        style="height:20.0px;width:122.0px;font-weight:bold;font-size:10pt;font-family:verdana"
                        required="true">


                        <xp:this.validators>
                            <xp:validateRequired message="Completarea localitatii este obligatorie.">
                            </xp:validateRequired>
                        </xp:this.validators>
                        <xp:typeAhead mode="full" minChars="1" ignoreCase="true"
                            id="typeAhead1">
                            <xp:this.valueList><![CDATA[#{javascript:@DbLookup(@DbName(),"vwLocalitati",Cdoc.txt_LocalitateCompanie,1,"[PARTIALMATCH]");}]]></xp:this.valueList>
                        </xp:typeAhead>
                        <xp:eventHandler event="onchange" submit="true"
                            refreshMode="norefresh">
                            <xp:this.action><![CDATA[#{javascript:Cdoc.setValue("dlg_Localitate",Cdoc.getValue("txt_LocalitateCompanie"));
Cdoc.setValue("dlg_Localitate_1",Cdoc.getValue("txt_LocalitateCompanie"))}]]></xp:this.action>

                            <xp:this.script><![CDATA[XSP.partialRefreshGet("#{id:txt_LocalitateS}", {
    onComplete: function() {
        XSP.partialRefreshGet("#{id:txt_LocalitateP}", {
            onComplete: function(){ } 
        });
    }
});]]></xp:this.script>
                        </xp:eventHandler>
                    </xp:inputText>

提前致谢

2 个答案:

答案 0 :(得分:1)

这里有两件事。首先,您应该禁用onChange事件的验证器,因此它不会显示验证错误。

其次,当您将CSJS脚本与SSJS一起使用时,它将触发CSJS,如果它返回true,则继续使用SSJS。因此,如果您希望CSJS代码在 SSJS之后运行,则可以将其放入oncomplete

如果我理解你的问题,下面的代码就可以解决它。

    <xp:inputText
        id="inputText1"
        value="#{Cdoc.txt_LocalitateCompanie}"
        style="height:20.0px;width:122.0px;font-weight:bold;font-size:10pt;font-family:verdana"
        required="true">
        <xp:this.validators>
            <xp:validateRequired
                message="Completarea localitatii este obligatorie.">
            </xp:validateRequired>
        </xp:this.validators>
        <xp:typeAhead
            mode="full"
            minChars="1"
            ignoreCase="true"
            id="typeAhead1">
            <xp:this.valueList><![CDATA[#{javascript:@DbLookup(@DbName(),"vwLocalitati",Cdoc.txt_LocalitateCompanie,1,"[PARTIALMATCH]");}]]></xp:this.valueList>
        </xp:typeAhead>
        <xp:eventHandler
            event="onchange"
            submit="true"
            refreshMode="norefresh"
            disableValidators="true">
            <xp:this.action><![CDATA[#{javascript:Cdoc.setValue("dlg_Localitate",Cdoc.getValue("txt_LocalitateCompanie"));
Cdoc.setValue("dlg_Localitate_1",Cdoc.getValue("txt_LocalitateCompanie"))}]]></xp:this.action>
            <xp:this.onComplete><![CDATA[if(dojo.byId("#{id:txt_LocalitateP}")) {
XSP.partialRefreshGet("#{id:txt_LocalitateP}", {
    onComplete: function() {
        XSP.partialRefreshGet("#{id:txt_LocalitateS}", {
            onComplete: function(){ } 
        });
    }
});
}]]></xp:this.onComplete>
        </xp:eventHandler>
    </xp:inputText>

更新:在您的情况下,您要刷新的字段位于带有partialRefresh="true"的第二个标签上。这意味着在partialRefreshGet时,DOM中可能不存在目标字段。我现在加了一张支票。

答案 1 :(得分:0)

这取自我的评论并提出答案:

由于性能和用户体验,onChange事件通常不受欢迎。但是,如果该字段是列出的控件,即组合框,则不是那么引人注目。可以使用以下选项/想法

  1. 取出onChange()来测试是否有所作为。如果是这样,请移动您的代码。
  2. 使用更新按钮整体更改所有字段,同时防止已输入的信息被不必要地删除
  3. 创建您自己的验证方法并手动显示/隐藏标签(hack-y)
  4. 研究如何手动将文本放入错误控件
  5. 如果该字段位于对话框中,请将onChange()移动到对话框的打开/关闭方法
  6. 进一步编辑 我可能建议使用xspDoc.getDocument(true)方法将所有更改从xpage推送到后台文档。有些东西告诉我,这可能会对服务器读取文档的更改并意识到它不是空的有所不同。

    其他创意
    我没有提到这个,因为它有点先进,但也应该在假设刷新完成后完成工作。即便如此,这也不是什么大不了的事。您可以将文档中的所有数据读入java bean。这个bean就是&#34;数据源&#34;对于您的页面,您将所有控件绑定到此bean的属性。然后,您将使用EL将控件绑定到bean。在那些触发其他字段更改的变量的setter中,更改这些值。所以,

    public PageBean(){
         //read connection information out of the URL and get the correct information out of the document
         //set all variables
         firstName=doc.getItemValueString("firstName");
    }
    
    private String firstName;
    public String getFirstName(){
         return firstName;
    }
    public void setFirstName(String firstName){
         this.firstName = firstName;
         setLastName("Schmidt");
    }
    ....
    

    使用faces-config.xml注册bean后,可以使用EL访问数据

    #{PageBean.firstName}
    #{PageBean.lastName}
    

    然后您可以在保存时再次获取文档并重置值,保存和释放。