XForms/显示保存结果
外观
< XForms
保存后,您希望在表单末尾显示一行,以确认结果已保存到服务器。您还希望在保存后通知用户任何错误。
我们将使用 submission 元素保存数据并返回保存结果。我们将使用 <xf:group> 来显示成功(绿色)或失败(红色)。
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fr="http://orbeon.org/oxf/xml/form-runner" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xf="http://www.w3.org/2002/xforms">
<head>
<title>Save Test</title>
<xf:model>
<xf:instance xmlns="" id="save-data">
<formdata form-id="unit-tests-save-test">
<first-name>Dan</first-name>
<last-name>McCreary</last-name>
</formdata>
</xf:instance>
<xf:instance xmlns="" id="save-results">
<save-results>
<message>No Save Results Yet</message>
</save-results>
</xf:instance>
<xf:submission id="save-submission" ref="instance('save-data')"
method="post" resource="https://127.0.0.1:8088/exist/rest/db/apps/grants/scripts/save.xq"
replace="instance"
instance="save-results">
<xf:action ev:event="xforms-submit-done">
<xf:message>Save Done</xf:message>
</xf:action>
<xf:action ev:event="xforms-submit-error">
<xf:message>Save Failed</xf:message>
</xf:action>
</xf:submission>
<xf:submission id="save-submission-see-save-results" ref="instance('save-data')"
method="post" resource="https://127.0.0.1:8088/exist/rest/db/apps/grants/scripts/save.xq" replace="instance"
instance="save-results">
<xf:action ev:event="xforms-submit-done">
<xf:message>Save Done</xf:message>
</xf:action>
<xf:action ev:event="xforms-submit-error">
<xf:message>Save Failed</xf:message>
</xf:action>
</xf:submission>
</xf:model>
<style type="text/css">
.success {color: green; background-color: lightgreen;}
.failure {color: red; background-color: pink;}
</style>
</head>
<body>
<xf:input ref="first-name">
<xf:label>First name:</xf:label>
</xf:input>
<br/>
<xf:input ref="last-name">
<xf:label>Last name:</xf:label>
</xf:input>
<a href="https://127.0.0.1:8088/exist/rest/db/apps/xforms/scripts/save.xq">Save URL</a>
<div class="save-controls">
<xf:submit submission="save-submission" appearance="xxforms:primary" class="button-margin-top">
<xf:label>Save and Validate</xf:label>
</xf:submit>
<xf:submit submission="save-submission-see-save-results" class="button-margin-top">
<xf:label>Save See Save Results</xf:label>
</xf:submit>
</div>
<xf:group ref="instance('save-results')/.[@code='200']">
<div class="success">
<xf:output ref="instance('save-results')//message"/>
</div>
</xf:group>
<xf:group ref="instance('save-results')/.[@code='400']">
<div class="failure">
<xf:output ref="instance('save-results')//message"/>
</div>
</xf:group>
</body>
</html>
xquery version "1.0";
import module namespace config= "http://danmccreary.com/config" at "../modules/config.xqm";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare namespace xf="http://www.w3.org/2002/xforms";
declare namespace dc="http://gov/grantsolutions/dc";
let $log := util:log-system-out('running save.xq')
(: get-data() returns the document node. We want the root node :)
let $formdata := request:get-data()/*
(: check to make sure we have valid post data :)
return
if (not($formdata))
then <save-results code="400">No Post Data</save-results>
else
let $save-data-collection := concat($config:app-home-collection, '/save-data')
let $form-id :=
if (exists($formdata/@code))
then $formdata/@code/string()
else
if (exists($formdata/@form-id))
then $formdata/@form-id/string()
else 'unknown-form-id'
let $file-name := concat($form-id, '.xml')
let $path-name := concat($save-data-collection, '/', $file-name)
let $overwrite :=
if (doc-available($path-name))
then true()
else false()
let $store := xmldb:store($save-data-collection, $file-name, $formdata)
return
<save-results code="200">
{if ($overwrite)
then <message>Document updated at {$path-name}</message>
else <message>New document saved to {$path-name}</message>
}
</save-results>