1.0.5 • Published 5 years ago

@i-novation/ngx-rapidforms v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Angular Rapidforms

A framework to shrink down the effort that is needed to get a form up and running in angular. It helps you to minimize your boilerplate code you would have to write for each form in your application. Also there are templates provided to give your application a clean and uniform look as well as displaying the same error messages throughout all forms.

What are the advantages?

  • minimized and clean code
  • minimize effort
  • easy to use
  • uniform design & error messages
  • better maintainability

Whats's the point?

Rapidforms utilizes you to create fast and simple forms for a simple way to interchange data. It is easy to use in new or existing projects. So you can use it besides known angular form techniques or completely rely on functions provided by this framework.

Requirements

Tested for versions of Angular >= 4.0. Compatible for npm >= 8

Installation

The recommended way of installing Rapidforms is to use npm. Therefore change to the root folder of your angular project and type

npm install @i-novation/ngx-rapidforms

and you are ready to go.

This will install the framwork in the node_modules folder using the latest version published on npm.

Usage

First steps

These steps will show you how to get up and running with the framework.

Import the module into your application

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component'; // Import the module import { RapidformsModule } from '@i-novation/ngx-rapidforms';

@NgModule({ declarations: AppComponent , imports: BrowserModule, // Add it to the imports RapidformsModule , providers: [], bootstrap: AppComponent }) export class AppModule { }

### Setting up a basic form

1. Add a 'dynamicform' element to your desired Template file (e.g. app.component.html)
```html
<div>
    <h1>Page title</h1>

    <dynamicform></dynamicform>
</div>
  1. Describe your form using an array
formRows1: DynamicFormRow[] = [
    new TextboxElement({
        key: "email",
        label: "Email",
        description: "Hint: This is also your username"
    }),
    new PasswordElement({
        key: "password",
        label: "Password"
    })
];
  1. Refer to the array in the HTML element
<dynamicform [rows]="formRows1"></dynamicform>
  1. Add a action method
public onSubmit(form: FormGroup) 
{
    console.log(form.controls.email.value);
}

You can refer to the element via their name you gave them as the key.

  1. Add that method to the HTML element
<dynamicform [rows]="formRows1" [onSend]="onSubmit"></dynamicform>

Add some validators

The values of each field can be validated based on rules defined by validators. You can either use an already existing one, or write one yourself.

formRows1: DynamicFormRow[] = [
    new TextboxElement({
        key: "email",
        label: "Email",
        description: "Hint: This is also your username",
        validators: [new EmailValidator()]
    }),
    new PasswordElement({
        key: "password",
        label: "Password",
        validators: [new RequiredValidator(), new MaxLengthValidator(30), new MinLengthValidator(8)]
    })
];

Add options to configure the form (optional)

//...
options1: DynamicFormOptions = new DynamicFormOptions({
    title: "Login",
    summarizedErrorMessage: false,
    placeholders: true,
    primaryButtonText: "Absenden"
});
//...
<dynamicform [rows]="formRows1" [onSend]="onSubmit" [options]="options1"></dynamicform>

Field types

componentclassusage
text boxTextboxElement.tsa regular text field
radio buttonRadioElement.tsselect one out of multiple values
passwordPasswordElement.tsspezialized field for passwords
textareaTextareaElement.tsmultiline text field
drop-downOptionElement.tsselect one out of multiple values
checkboxCheckboxElement.tsa regular checkbox
custom htmlCustomElement.tscan be used to insert custom html into the form

Structure and parameters

Each predefined field has these attributes

attributestandard valuedescription
valuevalue of the field
keyunique key of the field (used to access values)
labelvalue of keydisplayed text (label or placeholder)
controlTypesets the html element that is used to display the field
description''a descriptive text that is displayed near the field
classes''add custom css classes to the field
hasPlaceholdersdecides wether the html element has a placeholder property or not
validators[]add validators to the field to check thier values against specific rules

Special attributes

  • OptionElement.ts, options: {key, value}
new OptionElement({
    key: "country",
    label: "Land",
    options: [
        { key: "germany", value: "Germany" },
        { key: "austria", value: "Austria" },
        { key: "switzerland", value: "Switzerland" }
    ]
})
  • RadioElement.ts, options: {key, value}
new RadioElement({
    key: "country",
    label: "Land",
    options: [
        { key: "germany", value: "Germany" },
        { key: "austria", value: "Austria" },
        { key: "switzerland", value: "Switzerland" }
    ]
})

Arrangement / Columns

To place elements in one row, a DynamicFormElementGroup can be used. The correspondig css classes have to be added. It can also be used to structure the form.

new DynamicFormElementGroup({
	fields: [
        new TextboxElement({
			key: "username",
			label: "Nutzername"
		}),
		new PasswordElement({
			key: "password",
			label: "Passwort"
		})
    ]
});

Validation

If a validator offers a dynamic attribute it can be used in the error message.

Each validator automatically has the dynamic attributes {attribute} und {value}.

validatordynamic attributesusage
BooleanValidator.ts{trueValue}, {falseValue}validates if the value has one of two allowed values
EmailValidator.tschecks an e-mail
IntegerValidator.tschecks for an integer
IPValidator.tsvalidates based on an IPv4 IP adress
MatchValidator.tscompares the value to the value of the field in the parameter
MaxLengthValidator.ts{maxLength}checks if the value is longer than desired
MinLengthValidator.ts{minLength}checks if the value is shorter than desired
NumberValidator.tschecks a number (with seperators)
RequiredValidator.tschecks if the field has a value
UrlValidator.tschecks of the field is a formal valid URL

Configuration

Overview of the configuration array

valuestandard valuedescription
title""set the title of the form
summarizedErrorMessagefalseshould error messages be summarized
placeholdersfalseshould placeholders be displayed instead of labels
primaryButtonText""sets the text of the submit button

Example:

//...
options1: DynamicFormOptions = new DynamicFormOptions({
    title: "Check-Out",
    summarizedErrorMessage: false,
    placeholders: true,
    primaryButtonText: "Submit"
});
//...

Global configuration

A global configuration can be set to be used for each form. If a option element is passed to a form, those values will be used to overwrite the standard values

    DynamicFormComponent.setDefaultOptions(this.options1);

License

The project is licensed under the MIT license.

1.1.0-beta.2

5 years ago

1.1.0-beta.1

5 years ago

1.1.0-beta.0

5 years ago

1.0.12

5 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago