πŸ’» AppML Client-Side Programming
Estimated reading: 5 minutes 36 views

πŸ“ AppML – Forms and Form Validation: Build Smart Forms Without JavaScript

🧲 Introduction – Why Use AppML for Forms?

Forms are the core of user input across the web. Whether it’s signing up users, collecting feedback, or processing ordersβ€”forms are essential. With AppML, you can build smart, validated forms using just HTML + JSON + Models. No JavaScript or backend code is required for basic setups.

🎯 What you’ll learn:

  • Create dynamic forms using AppML
  • Use appml-model to apply validation rules
  • Bind form input to data sources with appml-data
  • Handle submission using appml-submit
  • Show success/error messages with appml-message

πŸ”§ Step-by-Step: Create a Simple Contact Form

πŸ“ Model File – models/contact-model.json

{
  "key": "id",
  "fields": [
    { "name": "name", "required": true },
    { "name": "email", "required": true },
    { "name": "message", "required": true }
  ]
}

πŸ“Œ Explanation:

  • "key": "id" – Unique identifier for each contact entry.
  • "fields" – Defines each input field:
    • name, email, message – All marked "required": true, so the form will not submit if any of them are empty.

πŸ“„ Data File – data/contacts.json

[
  {
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com",
    "message": "Hi, this is great!"
  }
]

πŸ“Œ Explanation:

  • This JSON array stores previously submitted form data.
  • AppML will fetch and append to this file when form data is submitted (if backend logic is implemented).

πŸ–₯️ HTML File – contact.html

<div 
  appml-model="models/contact-model.json" 
  appml-data="data/contacts.json" 
  appml-message>
  
  <h2>Contact Us</h2>
  
  <input name="name" placeholder="Your Name">
  <input name="email" type="email" placeholder="Your Email">
  <textarea name="message" placeholder="Your Message"></textarea>
  
  <button appml-submit>Send</button>
</div>

πŸ“Œ Explanation (line-by-line):

  • <div> – This is the AppML form container.
    • appml-model="..." links to the model that defines field rules.
    • appml-data="..." binds this form to the contacts data file.
    • appml-message allows AppML to show success/error messages here.
  • <input name="name"> – Binds user name input to the name field in the model.
  • <input name="email" type="email"> – Binds to the email field and uses HTML5 validation.
  • <textarea name="message"> – Binds to the message field.
  • <button appml-submit> – Submits the form. AppML will validate and handle the data automatically.

πŸ” Example: Order Form With Number Validation

πŸ“ Model File – models/order-model.json

{
  "key": "id",
  "fields": [
    { "name": "product", "required": true },
    { "name": "quantity", "required": true, "datatype": "number" }
  ]
}

πŸ“Œ Explanation:

  • Two fields:
    • product – Must be filled out.
    • quantity – Required and must be a number.

πŸ–₯️ HTML Order Form – order.html

<div 
  appml-model="models/order-model.json" 
  appml-data="data/orders.json" 
  appml-message>
  
  <h2>Place Your Order</h2>
  
  <input name="product" placeholder="Product Name">
  <input name="quantity" type="number" placeholder="Quantity">
  
  <button appml-submit>Order Now</button>
</div>

πŸ“Œ Explanation:

  • appml-model="..." loads validation for product and quantity.
  • appml-data="..." binds to orders.json, where form entries will be stored.
  • input name="product" – Tied to the product field.
  • input name="quantity" – Must be a number due to the model rule and type="number".

βœ… Add Real-Time Validation with a Controller

🧠 JavaScript File – controllers/contact.js

myAppML.onvalidate = function() {
  if (!myAppML.data.email.includes("@")) {
    myAppML.message = "Invalid email format.";
    return false;
  }
  return true;
};

πŸ“Œ Explanation:

  • onvalidate is called before submission.
  • myAppML.data.email checks if the input contains an @ symbol.
  • myAppML.message displays an error if validation fails.
  • return false blocks submission.

πŸ’¬ Add Controller to HTML

<div 
  appml-model="models/contact-model.json" 
  appml-data="data/contacts.json" 
  appml-controller="controllers/contact.js"
  appml-message>
  
  <!-- Form contents -->
</div>

πŸ“Œ Adds controller logic to the form. AppML will auto-run the script on validation.


🧰 AppML Form Attribute Overview

AttributeDescription
appml-modelSpecifies validation rules via a JSON model file
appml-dataBinds form data to a backend endpoint or JSON file
appml-controllerAdds advanced logic for validation or processing
appml-submitSubmits the form and triggers validation
appml-messageDisplays success or error messages

πŸ“Œ Summary – Recap & Key Takeaways

AppML lets you build complete, validated forms using HTML-only syntax powered by declarative data and model files. You can handle user input, validate it, and manage messages all without custom JavaScript.

πŸ” Key Takeaways:

  • Define validation rules in appml-model
  • Bind your forms to data sources using appml-data
  • Submit and process data with appml-submit
  • Display feedback with appml-message
  • Add extra validation with appml-controller if needed

βš™οΈ AppML forms are perfect for contact pages, surveys, admin panels, and classroom appsβ€”offering low-code power with high usability.


❓ FAQs – AppML Forms and Validation


❓ Can I submit the form to a backend script like PHP?
βœ… Yes. Use a server-side appml-data endpoint (e.g., data.php) instead of a static JSON file.


❓ Does HTML5 validation work together with AppML?
βœ… Yes. Browser validation (e.g., type="email") complements AppML model rules.


❓ How do I reset a form after submission?
βœ… Use a <button type="reset">Reset</button> or script it with AppML’s controller if needed.


❓ Can I style appml-message output?
βœ… Absolutely. Use custom CSS to format messages (e.g., error in red, success in green).


❓ Does AppML support dropdowns and radio buttons?
βœ… Yes. All standard HTML inputs work and bind using the name attribute and model fields.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

AppML – Forms and Form Validation

Or Copy Link

CONTENTS
Scroll to Top