Form

Overview of the Form Component

Form Component

Handling user input through forms is fundamental to many applications. Forms enable users to log in, update profiles, enter sensitive information, and perform various data-entry tasks.

The Form component is based on Angular template-driven forms.

Composition

The Form is a container that can include Form Controls such as Checkbox, Datetime, Input, Radiogroup, Range, Select, and Toggle.

To create a Form Control, drag and drop a component from the PALETTE and place it inside the Form. You'll then see the Form Control section with additional control properties:

🚧

Important

A component becomes a Form Control only when the Name property is specified, which defines the control's data key.

For example, if you have an Input with Name = userName and value = Peter, the control data will be available as a property of the form data object: {userName: 'Peter'}.

Sending Data

There are two ways to send form data:

  1. Using a Submit control - The control must be placed inside the form:
<form>
    <ion-button type="submit">Submit</ion-button>
</form>
  1. Using the Form submit action - Available on the EVENTS tab. The control can be placed anywhere on the page.

Form Submission Actions

Actions for form submission can be specified in three ways:

  1. Form submit event (for beginners) - Use the form component's Form submit event on the EVENTS tab. Choose an action to execute when the form is submitted, such as Invoke service (e.g., Login service) with specified mapping on the DATA panel.

  2. (ngSubmit) event (for advanced users) - Define a custom function handler that executes the submission from custom code.

  3. Action property (for advanced users) - Define the URL where form data will be sent.

Validation

By default, Ionic provides built-in validation rules for form controls. To use the form's overall validity, specify the Form ID property.

The value passed as the Form ID property can be used as a variable containing form information, as described in the PROPERTIES section. The Form ID property can be used as a variable containing the Form state (e.g., formID.valid and formID.value). To use it in custom code, add the 'this.' prefix.

👍

Validation Component

For UI components that don't automatically get validation properties when included in a Form, or are difficult to combine for validation purposes, use the Validation component.


👍

Datetime Validation Customization Video Tutorial

You can also check this YouTube video to learn how to use the plugin, make date fields required and ensure user input, and see how the Validation component works within the Form container for reliable datetime checks:

Form and Control States

Form and Controls have different states when using Ionic validation:

StateDescription
validA control is considered valid if no validation errors exist with the current value. Returns null if the control is not present.
invalidAn error exists in the input value. Returns null if the control is not present.
pendingAsync validation is occurring and errors are not yet available for the input value. Returns null if the control is not present.
disabledThe control is disabled in the UI, exempt from validation checks, and excluded from aggregate values of ancestor controls. Returns null if the control is not present.
enabledThe control is included in ancestor calculations of validity or value. Returns null if the control is not present.
pristineThe user has not yet changed the value in the UI. Returns null if the control is not present.
dirtyThe user has changed the value in the UI. Returns null if the control is not present.
touchedThe user has triggered a blur event on it. Returns null if the control is not present.
untouchedThe user has not yet triggered a blur event on it. Returns null if the control is not present.
errorsReports the control's validation errors.

Validation Examples

You can check the invalid input state to show an error message:

<ion-text *ngIf="user.invalid" color="danger">Incorrect user name</ion-text>

In this example, user is a Control ID defined for the Input component.

For specific validation errors, use the errors state with the validation error type.

Here's how to display different error messages for Input component validation:

  1. Define the Control ID property of the Input as passControl
  2. Set validation rules (e.g., required and pattern="[0-9]*")
  3. Add error message components with the *ngIf directive
<form #myForm="ngForm">
    <ion-input required pattern="[0-9]*" #passControl="ngModel" [ngModel] name="pass"></ion-input>
    <ion-text *ngIf="passControl.errors && passControl.errors.required" color="danger">
        Value must be entered
    </ion-text>
    <ion-text *ngIf="passControl.errors && passControl.errors.pattern" color="danger">
        Value should contain only numbers
    </ion-text>
</form>

If the component is valid, passControl.errors returns null (equivalent to false). If invalid, it returns an object containing all validation errors accessible by their names.

📘

Want to know more?

More details with examples about the Validation property can be found here.

👍

Datetime Validation Customization Video Tutorial

You can also check this YouTube video to learn how to use the plugin, make date fields required and ensure user input, and see how the Validation component works within the Form container for reliable datetime checks:

Properties

🚧

Important Note

This document lists properties specific to this UI component. For properties common to most UI components, see the General components document.

Common Properties

Property NameProperty Description
NovalidateThis boolean attribute indicates that the form shouldn't be validated when submitted.
Native validationIf set to True, native HTML5 validation is used instead of Angular validation. The form will work as the default HTML <form>.
Form IDA unique word identifying the component (Template reference variable) that contains current form information. Used in component properties as a variable containing the form state.
Update OnControls when the value or validity is updated: Change, Blur, Submit
FeaturesAdvanced options containing default HTML form attributes:

Action - The URL that processes the form submission. Specifies where to send form data when submitted. Disabled if Form submit or (ngSubmit) is used.

Enctype - Specifies how form data should be encoded when submitting to the server (only if Method is set to post):
- application/x-www-form-urlencoded - Default. All characters are encoded before sending (spaces become "+" symbols, special characters become ASCII HEX values)
- multipart/form-data - No characters are encoded. Required for forms with file upload controls
- text/plain - Spaces become "+" symbols, but no special characters are encoded

Method - Specifies the HTTP method (get or post) for sending form data:
- get - Appends form data to the URL in name/value pairs. Limited to about 3000 characters. Never use for sensitive data (visible in URL). Better for non-secure data like query strings. Useful when users want to bookmark results.
- post - Appends form data inside the HTTP request body (not visible in URL). No size limitations. Cannot be bookmarked.

Target - Specifies where to display the response after form submission:
- _blank - Response displayed in new window or tab
- _self - Response displayed in same frame (default)
- _parent - Response displayed in parent frame
- _top - Response displayed in full body of window
(ngSubmit)The event emitted when the form is submitted. The Form submit event takes precedence over the Action property.
🚧

Note

In the component's initial state, default HTML <form> features (Action, Target, etc.) are not supported because they're replaced with Angular functionality. To disable Angular functionality and enable default HTML behavior, enable the Native validation property.


Using Form ID

The Form ID property can be used in templates or code as a variable containing information about the form:

formID = {
    value: boolean,   // Reports the value of the form
    valid: boolean,   // Reports whether the form is valid
    invalid: boolean, // Reports whether the form is invalid
    dirty: boolean    // Reports whether the form is dirty
}

In templates, use the Form ID property as a variable. It can also be passed as a function argument:

<ion-button disabled={{formID.invalid}} type="submit">Submit</ion-button>
<ion-button (click)="send(formID)">Send</ion-button>

To use in custom code, the variable is available as this.<Form ID>:

if (this.formID.valid) this.submit();