Welcome to CodeYourCraft's XForms Input tutorial! In this comprehensive guide, we'll dive into XML Interactive Forms, a powerful tool for creating user-friendly web applications. By the end of this lesson, you'll be well-equipped to create your own XForms! 📝
XForms is an XML-based markup language for creating interactive forms, designed to simplify and enhance web-based data collection and validation. XForms allows you to create complex, dynamic forms, and handle user input with ease.
xform: The root element that encloses the entire XForms documentmodel: The container for data and data bindingsinstance: Represents the actual data used in the formbind: Establishes a connection between an instance variable and an elementsubmit: Defines the action to be performed when the form is submittedaction: Specifies the action to be taken when an event occurstrigger: Initiates an actioncontrol: Represents a form control, such as a text box or a dropdown listLet's create a simple XForms example: a form for collecting a user's name and email address.
<xform xmlns="http://www.w3.org/2002/xforms">
<model>
<instance>
<user>
<name/>
<email/>
</user>
</instance>
</model>
<view>
<h1>User Registration Form</h1>
<label>Name:</label>
<input ref="user/name"/>
<br/>
<label>Email:</label>
<input ref="user/email"/>
<br/>
<submit>Submit</submit>
</view>
</xform>In this example, we have created a simple XForms document with a form for collecting a user's name and email address. We defined an instance with two empty fields, name and email, and then created input controls for each field.
Now that we have our form, let's handle the submission:
<xform xmlns="http://www.w3.org/2002/xforms">
<!-- ... (the same form as before) ... -->
<actions>
<action ev:event="xforms-submit" execute="submit-action"/>
</actions>
<bind id="submit-action" nodeset="/user" submit="submit-success">
<xsl:when test="count(*) > 0">
<xsl:value-of select="/user"/>
</xsl:when>
</bind>
<template match="/">
<h1>Submission Successful</h1>
</template>
</xform>In this example, we added an actions section, which defines an action to be executed when the form is submitted (xforms-submit event). The action references a function submit-action, which checks whether the user instance contains any data. If it does, the function outputs the entire user instance. We also created a template to display the submission success message.
What is XForms used for?
Now that you've learned the basics of XForms, it's time to dive deeper! You can explore advanced features like repeatable sections, validation, and dynamic controls. Happy coding! 💡