Svelte Smart Form is a powerful, flexible, and easy-to-use form builder component for Svelte applications. It allows you to dynamically generate forms based on a configuration object, handling validation and submission with minimal effort.
To use Svelte Smart Form in your project:
Copy the SmartForm.svelte
component into your project's components directory.
Import and use the SmartForm component in your Svelte file:
<script>
import SmartForm from './path/to/SmartForm.svelte';
const fields = [
{ name: 'username', type: 'text', label: 'Username', required: true },
{ name: 'email', type: 'email', label: 'Email', required: true },
// Add more fields as needed
];
function handleSubmit(event) {
console.log('Form submitted:', event.detail);
}
</script>
<SmartForm {fields} on:submit={handleSubmit} />
Customize your form by modifying the fields
array.
Handle form submission in the handleSubmit
function.
Each field in the fields
array can have the following properties:
name
(required): The name of the fieldtype
(required): The type of input (e.g., 'text', 'email', 'password', 'checkbox', 'radio', 'select', 'textarea')label
(required): The label for the fieldrequired
(optional): Whether the field is requiredoptions
(required for 'radio' and 'select'): An array of options for radio buttons or select dropdownsvalidate
(optional): A custom validation functionminLength
(optional): Minimum length for text inputsmaxLength
(optional): Maximum length for text inputspattern
(optional): A regex pattern for validationSvelte Smart Form also includes a Form Builder component that allows you to create forms visually and generate the corresponding code:
Copy the SmartFormBuilder.svelte
component into your project's components directory.
Import and use the SmartFormBuilder component in your Svelte file:
<script>
import SmartFormBuilder from './path/to/SmartFormBuilder.svelte';
</script>
<SmartFormBuilder />
Svelte Smart Form comes with basic styling out of the box. You can easily customize the appearance by overriding the CSS classes in your Svelte component.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)This project is licensed under the MIT License - see the LICENSE.md file for details.
If you encounter any problems or have any questions, please open an issue on the GitHub repository.
Made with ❤️ by Leon Kalema