跳转到内容

XForms/服务器端字段验证

来自维基教科书,自由的教科书

步骤 1: 添加用于发送和接收数据的实例

[编辑 | 编辑源代码]
<!-- store the outgoing data for the invalid e-mail check -->
<xf:instance id="email-out">
   <data xmlns="">
      <email></email>
   </data>
</xf:instance>


<!-- place to store the results of an invalid e-mail check -->
<xf:instance id="email-check-results">
   <data xmlns=""/>
</xf:instance>

步骤 2: 在模型中添加一个提交元素

[编辑 | 编辑源代码]

提交元素将“字段更改”事件连接到服务器上的服务。

<xf:submission id="email-check" method="get" action="email-check.xq" 
           ref="instance('email-check')" 
           replace="instance" instance="email-check-results" />

步骤 3: 在输入中添加一个操作

[编辑 | 编辑源代码]
<xf:input ref="email" >
    <xf:label>Email: </xf:label>
    <xf:hint>This field must contain a valid email format.</xf:hint>
    <xf:action ev:event="xforms-value-changed">
           <!-- copy from your "save-data" into the outgoing instance -->
           <xf:setvalue ref="instance('email-out')/email" value="instance('save-data')/email"/>
           <xf:send submission="email-check"/>
    </xf:action>
</xf:input>

步骤 4: 添加一个组,如果结果无效则显示错误

[编辑 | 编辑源代码]
<xf:group ref="instance('email-check-results')/message[@class='error']">
     <div class="field-warn"><xf:output value="instance('email-check-results')/message"/></div>
</xf:group>

步骤 5: 创建一个返回 true/false 的服务器端 REST GET 服务

[编辑 | 编辑源代码]
If there are no errors just return <ok/>

If there are errors return
<message type="error">Invalid e-mail format</message>
华夏公益教科书