mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 22:04:56 +00:00
update: form,packages,lua (3 files)
This commit is contained in:
@@ -1,90 +1,4 @@
|
||||
-- Contact form configuration and logic
|
||||
local validate = require("validate")
|
||||
local json = require("json")
|
||||
-- This file has been split into smaller modules in contact_form/
|
||||
-- Use require("contact_form") or the individual submodules instead
|
||||
|
||||
---@class ContactForm
|
||||
local M = {}
|
||||
|
||||
---@class FormField
|
||||
---@field name string
|
||||
---@field label string
|
||||
---@field type string
|
||||
---@field required? boolean
|
||||
|
||||
---@class FormConfig
|
||||
---@field fields FormField[]
|
||||
---@field successMessage string
|
||||
|
||||
---@class FormState
|
||||
---@field [string] string
|
||||
|
||||
---@class FormValidation
|
||||
---@field valid boolean
|
||||
---@field errors table<string, string>
|
||||
|
||||
---@class SubmitResult
|
||||
---@field success boolean
|
||||
---@field message? string
|
||||
---@field errors? table<string, string>
|
||||
---@field data? FormState
|
||||
|
||||
-- Load config from JSON file
|
||||
---@type FormConfig
|
||||
M.config = json.load("contact_form.json")
|
||||
|
||||
-- Create initial empty form state
|
||||
---@return FormState
|
||||
function M.createInitialState()
|
||||
local state = {}
|
||||
for _, field in ipairs(M.config.fields) do
|
||||
state[field.name] = ""
|
||||
end
|
||||
return state
|
||||
end
|
||||
|
||||
-- Validate the contact form
|
||||
---@param formValues FormState
|
||||
---@return FormValidation
|
||||
function M.validate(formValues)
|
||||
local errors = {}
|
||||
local emailPattern = "^[^%s@]+@[^%s@]+%.[^%s@]+$"
|
||||
|
||||
for _, field in ipairs(M.config.fields) do
|
||||
local value = formValues[field.name]
|
||||
value = value and string.gsub(value, "^%s*(.-)%s*$", "%1") or ""
|
||||
|
||||
if field.required and value == "" then
|
||||
errors[field.name] = field.label .. " is required"
|
||||
elseif field.type == "email" and value ~= "" then
|
||||
if not string.match(value, emailPattern) then
|
||||
errors[field.name] = "Enter a valid email address"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return { valid = next(errors) == nil, errors = errors }
|
||||
end
|
||||
|
||||
-- Handle form submission
|
||||
---@param formValues FormState
|
||||
---@return SubmitResult
|
||||
function M.handleSubmit(formValues)
|
||||
local validation = M.validate(formValues)
|
||||
|
||||
if not validation.valid then
|
||||
return {
|
||||
success = false,
|
||||
errors = validation.errors
|
||||
}
|
||||
end
|
||||
|
||||
log("Contact form submitted: " .. (formValues.email or "unknown"))
|
||||
|
||||
return {
|
||||
success = true,
|
||||
message = M.config.successMessage,
|
||||
data = formValues
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
return require("contact_form.init")
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
-- Handle form submission
|
||||
require("contact_form.types")
|
||||
local json = require("json")
|
||||
local validate_module = require("contact_form.validate")
|
||||
|
||||
---@class HandleSubmitModule
|
||||
local M = {}
|
||||
|
||||
---@type FormConfig
|
||||
local config = json.load("contact_form.json")
|
||||
|
||||
---@param formValues FormState
|
||||
---@return SubmitResult
|
||||
function M.handleSubmit(formValues)
|
||||
local validation = validate_module.validate(formValues)
|
||||
|
||||
if not validation.valid then
|
||||
return {
|
||||
success = false,
|
||||
errors = validation.errors
|
||||
}
|
||||
end
|
||||
|
||||
log("Contact form submitted: " .. (formValues.email or "unknown"))
|
||||
|
||||
return {
|
||||
success = true,
|
||||
message = config.successMessage,
|
||||
data = formValues
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
19
packages/form_builder/seed/scripts/contact_form/init.lua
Normal file
19
packages/form_builder/seed/scripts/contact_form/init.lua
Normal file
@@ -0,0 +1,19 @@
|
||||
-- Contact form module
|
||||
require("contact_form.types")
|
||||
local json = require("json")
|
||||
|
||||
---@class ContactFormModule
|
||||
---@field config FormConfig
|
||||
---@field createInitialState fun(): FormState
|
||||
---@field validate fun(formValues: FormState): FormValidation
|
||||
---@field handleSubmit fun(formValues: FormState): SubmitResult
|
||||
|
||||
---@type ContactFormModule
|
||||
local M = {
|
||||
config = json.load("contact_form.json"),
|
||||
createInitialState = require("contact_form.create_initial_state").create,
|
||||
validate = require("contact_form.validate").validate,
|
||||
handleSubmit = require("contact_form.handle_submit").handleSubmit
|
||||
}
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user