π AppML β Form-Driven Models: Build Dynamic Forms with Structured Logic
π§² Introduction β What Are Form-Driven Models in AppML?
AppML simplifies form handling by linking HTML forms directly with models that define field behavior, validation rules, and database relationships. These are called form-driven models. With them, you can build complete, interactive forms for Create, Read, Update, and Delete (CRUD) operationsβwithout any JavaScript!
π― In this article, youβll learn:
- How form-driven models work in AppML
- How to connect forms to models and data sources
- Use cases for auto-generated forms (CRUD apps, admin panels)
- Validation, field constraints, and controller enhancements
π§© What Is a Form-Driven Model?
A form-driven model in AppML is a .json
or .xml
file that defines:
- Fields (name, datatype, required)
- Input controls (text, number, dropdown, etc.)
- Validation logic
- Default values and display formats
- Related database table
When used with appml-model
, AppML auto-generates form fields, applies validation, and binds data to the backend.
π§± Sample JSON Model β models/user-model.json
{
"tablename": "users",
"orderby": "name",
"fields": [
{ "name": "id", "primarykey": true },
{ "name": "name", "required": true },
{ "name": "email", "required": true },
{ "name": "role", "default": "Viewer" }
]
}
β This model:
- Sets βusersβ as the database table
- Marks βnameβ and βemailβ as required fields
- Defines βroleβ with a default value
π§ͺ Example β HTML Form Bound to a Model
<div appml-model="models/user-model.json" appml-data="get-users.php">
<input name="name">
<input name="email">
<select name="role">
<option>Admin</option>
<option>Editor</option>
<option>Viewer</option>
</select>
<button appml-submit>Save</button>
</div>
π With this setup, AppML:
- Loads data
- Maps fields to the model
- Enforces validation on submit
- Sends data to the backend (via POST)
π AppML Form Attributes
Attribute | Purpose |
---|---|
appml-model | Connects HTML form to a data model |
appml-data | Loads existing records into form |
appml-submit | Triggers form submission |
name="..." | Binds input field to a model property |
β¨ Custom Validation with Controller
Add business logic validation before submission:
β
user-controller.js
myAppML.onvalidate = function() {
if (!myAppML.data.email.includes("@")) {
alert("Enter a valid email.");
return false;
}
return true;
};
This complements the static validation defined in your model file.
π CRUD with Form-Driven Models
AppML + form-driven models = full CRUD support:
Operation | How It Works |
---|---|
Create | Leave id blank and click Save |
Read | AppML loads data via appml-data |
Update | Edit form and click Save |
Delete | Call myAppML.deleteRecord() in a controller |
π‘ Tip β Dynamic Dropdowns from Data Files
You can populate a <select>
from a JSON list:
β HTML
<select name="category" appml-data="data/categories.json">
<option>{{name}}</option>
</select>
β
data/categories.json
[
{ "name": "Admin" },
{ "name": "User" },
{ "name": "Guest" }
]
π§ Auto-generates dropdown options based on the datafile.
π Summary β Recap & Key Takeaways
Form-driven models are at the heart of AppMLβs low-code strategy. They automate form creation, validation, and bindingβso you can focus on building features, not wiring logic.
π Key Takeaways:
- Form-driven models define field structure, rules, and defaults
- Auto-generate form controls using
appml-model
+name="..."
- Easily build CRUD forms without JS frameworks
- Combine with controllers for custom validation and actions
βοΈ Use form-driven models to rapidly create admin panels, signup forms, profile editors, and more.
β FAQs β Form-Driven Models in AppML
β Do I need to write JavaScript for form validation?
β Not always. Use required
, datatype
, and default
in your model. Use onvalidate
only for advanced logic.
β Can I submit the form to a backend like PHP?
β
Yes. AppML automatically sends form data to the appml-data
URL for POST/PUT operations.
β How do I clear the form after save?
β
Use myAppML.clear()
after submission inside onaftershow
or onsubmit
.
β Can I use dropdowns with dynamic values?
β
Yes! Use appml-data="..."
on a <select>
element and bind via {{field}}
.
β Are model files required for all forms?
β οΈ Not strictly, but they are recommended for validation, default values, and future scalability.
Share Now :