PHP Classes

File: example/example.php

Recommend this page to a friend!
  Classes of Victor Bolshov   PHP Xforms   example/example.php   Download  
File: example/example.php
Role: Example script
Content type: text/plain
Description: example of usage of PhpXforms with XForms
Class: PHP Xforms
Validate XML documents using XML Schema
Author: By
Last change:
Date: 16 years ago
Size: 6,788 bytes


Class file image Download

 * PhpXforms usage example.
 * The example should be run in FireFox with XForms extension installed.
 * (Possibly will run in X-Smiles)
 * @author Victor Bolshov <>
 * @package PHP_XFORMS
 * @subpackage EXAMPLE

function fake_validation_callback($data) {
    if (
rand(0, 100) > 50) throw new Exception("Server-side validation error!");

if (
dirname(__FILE__) . '/../PhpXforms.php';
$px = new PhpXforms(dirname(__FILE__) . '/schema.xsd');

header('Content-Type: application/xhtml+xml; charset=utf-8');
"<?xml version='1.0' encoding='utf-8' ?>\n";

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
    <title>PhpXforms Example</title>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
<h1>PhpXforms Example</h1>

 <!--The model.
 This is the heart-concept of XForms, which allows for separating business-logic from
 Validation of data on client/server is done with a help of XMLSchema
 (pay attention to the "schema" attribute) -->
<xf:model id="theOnlyModel" schema="schema.xsd">

    <!-- 1. Instance documents.
     Instance documents are arbitrary XML documents,
     containing data that the form will work with.
     The instance documents may be specified as URLs,
     but in this example I use inline documents -->

    <!-- 1.1. errorList instance data are not submitted.
     They are used just to provide the user with server-side validation/processing error messages
     (if any) -->
    <xf:instance id="errorList" xmlns="">
        <errorList />

    <!-- 1.2. This is our main instance.
     The data from this instance will be submitted to server,
     validated and processed -->
    <xf:instance id="data" xmlns="">

    <!-- 2. The binds.
     Here we bind data pieces (specified by the "nodeset" attribute, with a help of XPath expressions) -
     to their datatypes. We may also specify a condition on which a data piece is required
     (see XForms specification or "XForms Essentials" book by Micah Dubinko for more details)
         id="publishdate" - we may reference this bind with IDref "publishdate"
         nodeset="instance('data')/publishdate" - defines data to be bound (here we bind <publishdate>
                                                     element from instance with id="data")
         type="xs:date" - XMLSchema type
         required="true()" - this data piece is alwayse required.
                                                     (notice the parethesis, and remember you
                                                     can specify any XPath expression here) -->
    <xf:bind id="publishdate" nodeset="instance('data')/publishdate" type="xs:date" required="true()" />
    <xf:bind id="title" nodeset="instance('data')/title" type="title" required="true()" />
    <xf:bind id="content" nodeset="instance('data')/content" type="content" required="true()" />

    <!-- 3. Submission.
     Here we define what, where and how to submit
         id="sendData" we may reference this submission with IDref "sendData"
         ref="instance('data')" - what to submit
         action="" - where to submit ("" means the current URL)
         method="post" - data transfer method
         mediatype="application/xml" - how to submit (well, actually, application/xml is the default so we could
                                         skip this attribute)
         includenamespaceprefixes="" - strip namespace definitions from submitted data (as I have noticed, this is often needed)
         replace="instance" - the data *read* from server should be a valida XML and they will replace a certain instance
                                         document in our model
         instance="errorList" - the instance to replace (see "replace" attribute) -->
    <xf:submission id="sendData"
        instance="errorList" />


<script language="javascript"><![CDATA[
 * Here is a little JavaScript code. I use something like this in my XForms,
 * because there are some limitations in W3C specifications for XForms and XMLEvents, so
 * I need a little scripting.
 * This function will be called after xforms-submit is done.
 * It will check the 'errorList' instance document in our model,
 * and, in case it finds no <error> elements below it,
 * it will show a modal window saying 'No server-side errors occured during validation'
 * The JS code uses features special for Mozilla XForms implementation!
function performOnSuccess() {
    var inst = document.getElementById('theOnlyModel').getInstanceDocument('errorList')// will work in Mozilla
    if (! inst.documentElement.getElementsByTagName('error').length)
        alert('No server-side errors occured during validation')
window.addEventListener('xforms-submit-done', performOnSuccess, false)

<div style="width:70%">

    <!-- Here is our XForms layout -->

    <!-- Display server-side validation/processing errors -->
    <ul class="errorList" id="errorListContainer">
        <xf:repeat nodeset="instance('errorList')/error">
            <li><xf:output ref="."/></li>

    <!-- Input controls -->
    <xf:input bind="publishdate" class="date">
        <xf:label>Example date field: </xf:label>

    <xf:input bind="title" class="text">
        <xf:label>Example title: </xf:label>
        <xf:action ev:event="xforms-invalid">
            <xf:message level="modal">Minimum 1, maximum 255 symbols</xf:message>

    <xf:textarea bind="content">
        <xf:label>Example text: </xf:label>

    <!-- And the submit button -->
    <xf:submit submission="sendData">


    <li>To run the example you will need Firefox web-browser with XForms extension.
        Both are free software, and you may download them at
        <a href=""></a> and
        <a href=""></a>
    <li>The server-side validation in this example is just a fake one. It will randomly generate an error or
        pass validation.</li>