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:
ImportantA 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 = userNameandvalue = 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:
- Using a Submit control - The control must be placed inside the form:
<form>
<ion-button type="submit">Submit</ion-button>
</form>- 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:
-
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.
-
(ngSubmit) event (for advanced users) - Define a custom function handler that executes the submission from custom code.
-
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 ComponentFor 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 TutorialYou 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:
| State | Description |
|---|---|
| valid | A control is considered valid if no validation errors exist with the current value. Returns null if the control is not present. |
| invalid | An error exists in the input value. Returns null if the control is not present. |
| pending | Async validation is occurring and errors are not yet available for the input value. Returns null if the control is not present. |
| disabled | The 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. |
| enabled | The control is included in ancestor calculations of validity or value. Returns null if the control is not present. |
| pristine | The user has not yet changed the value in the UI. Returns null if the control is not present. |
| dirty | The user has changed the value in the UI. Returns null if the control is not present. |
| touched | The user has triggered a blur event on it. Returns null if the control is not present. |
| untouched | The user has not yet triggered a blur event on it. Returns null if the control is not present. |
| errors | Reports 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:
- Define the Control ID property of the Input as
passControl - Set validation rules (e.g.,
requiredandpattern="[0-9]*") - Add error message components with the
*ngIfdirective
<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 TutorialYou 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 NoteThis document lists properties specific to this UI component. For properties common to most UI components, see the General components document.
Common Properties
| Property Name | Property Description |
|---|---|
| Novalidate | This boolean attribute indicates that the form shouldn't be validated when submitted. |
| Native validation | If set to True, native HTML5 validation is used instead of Angular validation. The form will work as the default HTML <form>. |
| Form ID | A 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 On | Controls when the value or validity is updated: Change, Blur, Submit |
| Features | Advanced 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. |
NoteIn 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();Updated 19 days ago
