Question

I am new to extension library REST services and xpages, I need to find out a way where I can validate user input data with servers data, to avoid server round trip, I am planning to use REST services.

I tried using something below but no luck, can any one please correct me where the below code is failing.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xe="http://www.ibm.com/xsp/coreex"
    xmlns:xc="http://www.ibm.com/xsp/custom">


    <xp:this.data>
        <xp:dominoDocument var="document1" formName="NavConfig"></xp:dominoDocument>
    </xp:this.data>
    <xe:restService id="restService1">

        <xe:this.service>
            <xe:documentJsonService compact="true"
                formName="NavConfig">
                <xe:this.querySaveDocument><![CDATA[#{javascript:var cmp=getComponent('menuItem1').getValue();
if (cmp=""){
getComponent('msg').setValue("Validation Failed")
}}]]></xe:this.querySaveDocument>
            </xe:documentJsonService>
        </xe:this.service>
    </xe:restService>
    <xp:br></xp:br>
    <xp:br></xp:br>
    <xp:text escape="true" id="msg"></xp:text>
    <xp:panel style="margin-left:20.0px;margin-top:20.0px">
        <xp:table>
            <xp:tr>
                <xp:td>
                    <xp:label value="Menu item:" id="menuItem_Label1"
                        for="menuItem1">
                    </xp:label>
                </xp:td>
                <xp:td>
                    <xp:inputText value="#{document1.MenuItem}"
                        id="menuItem1">
                    </xp:inputText>
                </xp:td>
            </xp:tr>
            <xp:tr>
                <xp:td>
                    <xp:label value="Link address:"
                        id="linkAddress_Label1" for="linkAddress1">
                    </xp:label>
                </xp:td>
                <xp:td>
                    <xp:inputText value="#{document1.linkAddress}"
                        id="linkAddress1">
                    </xp:inputText>
                </xp:td>
            </xp:tr>
        </xp:table></xp:panel>
    <xp:panel style="margin-top:20.0px;margin-left:20.0px">

        <xp:button value="Save" id="button2"><xp:eventHandler event="onclick" submit="true" refreshMode="complete">
    <xp:this.action>
        <xp:save></xp:save>
    </xp:this.action></xp:eventHandler></xp:button></xp:panel>
</xp:view>
Was it helpful?

Solution

REST by nature is a "dumb" API. I would rather say: elegant in its simplicity. You GET data from a server and you PUT/POST it back or DELETE it. So every time you talk to REST, you have a server round trip - that is REST how it is build.

In XPages when your client (JavaScript) is requesting data via REST, that service doesn't interact with any of the components on the page, so checking a menu or setting a component's text has no impact on the REST call.

When you want to do client side validation you need to implement that in client side script otherwise you do have a server round trip (which might not be that bad actually).

I would, to be most flexible, use a custom REST service (also provided by the rest control) to create your own JSON that contains only the items you actually need but adds info for your local validation code like {'requiredFields' : [{'name' : 'subject', 'message' : 'hey sorry, I need a subject to process this'}, {...}]} and a status reply when you POST data back.

Check the sample database and the book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top