Skip to content

A dynamic form control prototype that generates customizable, validated forms based on configuration objects in Angular.

Notifications You must be signed in to change notification settings

Prasadlokhande-880/DynamicFormGenration

Repository files navigation

Here's a sample README.md for your dynamic form control prototype project:


Dynamic Form Control Prototype

This project provides a dynamic form control generator prototype, which allows for the creation of customizable, validated forms based on provided configuration objects. The forms are created dynamically using Angular and can be easily integrated into various projects.

Features

  • Dynamic Form Generation: Generate forms based on provided configuration objects.
  • Custom Validators: Implement both standard and custom validators for fields.
  • Flexible Input Types: Supports various input types like text, number, date, select dropdowns, and checkboxes.
  • Built-in Validation: Automatically validates fields based on predefined rules.
  • Extensibility: Easy to extend with custom form fields, validators, and components.

Installation

To use this prototype in your project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/your-username/dynamic-form-control.git
  2. Navigate to the project directory:

    cd dynamic-form-control
  3. Install dependencies:

    npm install

Usage

  1. Import the FormModule: In your Angular project, import the FormModule from the prototype into your application module.

    import { FormModule } from 'dynamic-form-control';
  2. Configure the Form: Define the form configuration in your component as an array of form fields, each containing a field name, type, validation rules, and any other properties.

    const formConfig = [
      {
        type: 'text',
        name: 'firstName',
        label: 'First Name',
        validation: [Validators.required]
      },
      {
        type: 'number',
        name: 'age',
        label: 'Age',
        validation: [Validators.required, Validators.min(18)]
      },
      {
        type: 'select',
        name: 'gender',
        label: 'Gender',
        options: ['Male', 'Female', 'Other'],
        validation: [Validators.required]
      }
    ];
  3. Bind the Form: Use the form configuration in your template to dynamically render the form fields.

    <app-dynamic-form [fields]="formConfig"></app-dynamic-form>
  4. Handle Form Submission: The form can be submitted as usual, and the validation status will be managed automatically.

    onSubmit(formValue) {
      if (this.form.valid) {
        console.log('Form submitted:', formValue);
      }
    }

Custom Validators

You can easily add custom validators by creating a validator function that returns a ValidationErrors object. Here's an example of a custom email validator:

import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';

export function emailValidator(): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const email = control.value;
    const isValidEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(email);
    return isValidEmail ? null : { invalidEmail: true };
  };
}

Development

To run the development environment locally, follow these steps:

  1. Clone the repository and install dependencies (as mentioned above).

  2. Run the development server:

    ng serve
  3. Open your browser and navigate to http://localhost:4200.

Contributing

If you would like to contribute to this project, feel free to fork the repository and submit pull requests. Please ensure that any new features or bug fixes are accompanied by tests.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Make sure to adjust any references or instructions specific to your project if needed!

About

A dynamic form control prototype that generates customizable, validated forms based on configuration objects in Angular.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •