JavaScript implementations of network transports, cryptography, ciphers, PKI, message digests, and various utilities.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
jQuery-based utilities for manipulating web forms and serializing form data to structured JSON objects. These utilities handle complex form structures including nested objects and arrays through bracket notation.
Note: This module requires jQuery to be available and is primarily designed for browser environments.
Convert jQuery form elements to structured JSON objects with support for nested properties and arrays.
/**
* Serialize a form to a JSON object with nested property support
* @param input - jQuery form element to serialize
* @param sep - Property separator for nested objects (default: '.')
* @param dict - Dictionary for field name replacement (name=replacement)
* @returns Structured object representing the form data
*/
forge.form.serialize(input: jQuery, sep?: string, dict?: {[key: string]: string}): object;Usage Examples:
// Basic form serialization
const form = $('#myForm');
const data = forge.form.serialize(form);
// Custom separator for nested properties
const data = forge.form.serialize(form, '_');
// Field name replacement
const data = forge.form.serialize(form, '.', {
'oldFieldName': 'newFieldName',
'user_id': 'userId'
});Regular input fields are serialized as simple key-value pairs:
<input name="username" value="john">
<input name="email" value="john@example.com">Results in:
{
username: "john",
email: "john@example.com"
}Use dot notation or custom separators to create nested objects:
<input name="user.name" value="john">
<input name="user.profile.age" value="30">
<input name="settings.theme" value="dark">Results in:
{
user: {
name: "john",
profile: {
age: "30"
}
},
settings: {
theme: "dark"
}
}Use square bracket notation to create arrays:
<input name="tags[]" value="javascript">
<input name="tags[]" value="crypto">
<input name="tags[]" value="security">Results in:
{
tags: ["javascript", "crypto", "security"]
}Specify array indices explicitly:
<input name="users[0].name" value="alice">
<input name="users[0].role" value="admin">
<input name="users[1].name" value="bob">
<input name="users[1].role" value="user">Results in:
{
users: [
{ name: "alice", role: "admin" },
{ name: "bob", role: "user" }
]
}Combine objects, arrays, and simple fields:
<input name="title" value="My Form">
<input name="metadata.version" value="1.0">
<input name="metadata.tags[]" value="form">
<input name="metadata.tags[]" value="utility">
<input name="sections[0].title" value="Personal Info">
<input name="sections[0].fields[]" value="name">
<input name="sections[0].fields[]" value="email">
<input name="sections[1].title" value="Preferences">
<input name="sections[1].fields[]" value="theme">Results in:
{
title: "My Form",
metadata: {
version: "1.0",
tags: ["form", "utility"]
},
sections: [
{
title: "Personal Info",
fields: ["name", "email"]
},
{
title: "Preferences",
fields: ["theme"]
}
]
}The utility parses bracket notation to determine structure:
field[] - Append to arrayfield[index] - Set specific array indexfield[key] - Set object propertyfield[key][subkey] - Nested object propertiesUse the dict parameter to rename fields during serialization:
const form = $('#registrationForm');
const data = forge.form.serialize(form, '.', {
'user_name': 'username',
'user_email': 'email',
'pwd': 'password'
});
// Input field named 'user_name' becomes 'username' in output
// Input field named 'pwd' becomes 'password' in output// Complete form processing example
$(document).ready(function() {
$('#submitBtn').click(function() {
const formData = forge.form.serialize($('#userForm'), '.', {
'user_id': 'userId',
'created_at': 'createdAt'
});
// Send structured data to server
$.ajax({
url: '/api/users',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(formData),
success: function(response) {
console.log('User created:', response);
}
});
});
});This module depends on jQuery and is designed for browser environments. It uses jQuery's serializeArray() method internally and requires:
name attributes