FormManager
Advanced form management system for Now.js framework
Overview
FormManager is a comprehensive form management system that provides advanced features for handling forms, file uploads, validation, and AJAX submissions in Now.js applications.
Key Features
- AJAX form submissions with file upload support
- Built-in form validation
- CSRF protection
- File upload with size and type validation
- Integration with ElementManager
- Event handling and state management
- Support for existing forms via data attributes
- Error handling and notifications
Quick Start
<form id="demoForm" data-form data-ajax-submit="true">
<div class="ggrid">
<div class="block6">
<label>Name</label>
<input type="text" name="name" data-validate="required|minlength:3"
data-element class="g-input" placeholder="Enter your name">
</div>
<div class="block6">
<label>Email</label>
<input type="email" name="email" data-validate="required|email"
data-element class="g-input" placeholder="Enter your email">
</div>
<div class="block12">
<label>Photo</label>
<input type="file" name="photo" accept="image/*" data-element>
</div>
<div class="block12">
<button type="submit" class="button blue">Submit</button>
<button type="reset" class="button">Reset</button>
</div>
</div>
</form>
// Initialize FormManager
FormManager.init({
ajaxSubmit: true,
validateBeforeSubmit: true,
resetAfterSubmit: true,
maxUploadSize: 5 * 1024 * 1024 // 5MB
});
// Handle form events
EventManager.on('form:success', ({ formId, result }) => {
NotificationManager.success('Form submitted successfully!');
});
EventManager.on('form:error', ({ error }) => {
NotificationManager.error(error.message || 'Form submission failed');
});
Live Demo
Configuration Options
FormManager.init({
// Basic settings
debug: false, // Enable debug mode
ajaxSubmit: true, // Use AJAX for submissions
validateBeforeSubmit: true, // Validate before submit
resetAfterSubmit: false, // Reset form after successful submit
// UI/UX settings
focusOnError: true, // Focus first error field
scrollToError: true, // Scroll to first error
errorClass: 'has-error', // Error class name
successClass: 'has-success', // Success class name
// Upload settings
uploadTimeout: 30000, // Upload timeout in ms
maxUploadSize: 10485760, // Max file size (10MB)
allowedMimeTypes: [ // Allowed file types
'image/*',
'application/pdf',
// ...
],
// CSRF Protection
csrf: {
enabled: true,
tokenName: '_token',
headerName: 'X-CSRF-TOKEN',
cookieName: 'XSRF-TOKEN'
}
});
Validation Rules
FormManager supports both HTML5 validation attributes and custom validation rules via data attributes:
<!-- HTML5 Validation -->
<input type="email" required minlength="3" maxlength="50">
<!-- Custom Validation Rules -->
<input type="text" data-validate="required|minlength:3|pattern:[A-Za-z]+">
Available Validation Rules
required
- Field must not be emptyemail
- Must be valid email addressurl
- Must be valid URLdate
- Must be valid datenumber
- Must be numeric valuemin:value
- Minimum numeric valuemax:value
- Maximum numeric valueminlength:value
- Minimum string lengthmaxlength:value
- Maximum string lengthpattern:regex
- Match regular expressionequalTo:field
- Must match another field
API Reference
Methods
init(options)
- Initialize FormManagergetForm(formId)
- Get form instancegetValues(formId)
- Get form values as objectsetValues(formId, values)
- Set form valuesisValid(formId)
- Check if form is validisModified(formId)
- Check if form is modifiedreset(formId)
- Reset form to initial statedestroy(formId)
- Clean up form instance
Events
form:success
- Form submitted successfullyform:error
- Form submission failedform:validate
- Form validation completeform:reset
- Form reset complete