Angular and Ionic Forms
Forms for Angular and Ionic Framework
Forms for Angular and Ionic Framework
Forms for Angular and Ionic Framework
Forms - Validation
There are two approaches to implement and validate forms in Angular. You can use Mobiscroll forms with both approaches. Mobiscroll takes care of styling and showing the correct message when wired up correctly.
Template driven forms
With template driven validation, we can set up the whole validation in the template of our component using directives as seen in the first part of our example. The #templateForm="ngForm" template variable will hold the FormGroup. We can later check if the form is valid through this variable.
Use required and minlength directives to make the fields required and give the password the minimum 6 charachters long rule. Use the mobiscroll components [error] and [errorMessage] attributes.
To give each error and field a proper error message, set up a message object in the component, and wire them to the proper [errorMessage] attribute.
Follow a more detailed guide from the documentation.
Reactive forms
In the case of reactive forms, the rules are defined in typescript by importing the Validators. Create a form with the FormBuilder and set up the rules.
The error messages can be set up similarily to the template driven approach and referenced in the [errorMessage] attribute.
Follow a more detailed guide from the documentation.
import {
Component,
ViewChild
} from '@angular/core';
let now = new Date();
@Component({
selector: 'demo-app',
templateUrl: './app.component.html'
})
export class AppComponent {
gender: string = 'male';
formSettings = {
lang: ' ',
theme: ' ',
themeVariant: ' '
};
dateSettings: any = {
defaultValue: new Date(now.getFullYear() - 18, 0, 1),
min: new Date(now.getFullYear() - 100, now.getMonth(), now.getDate()),
max: new Date(now.getFullYear() - 13, now.getMonth(), now.getDate())
};
numpadSettings: any = {
display: 'bottom',
cssClass: 'md-phone-num',
template: '{plus}ddddddddddd',
maxLength: 14,
allowLeadingZero: true,
leftKey: {
text: '+',
value: '',
variable: 'plus:+'
},
formatValue: function (numbers, vars, inst) {
var newVal = '';
if (vars.plus) {
newVal += vars.plus;
}
newVal += numbers.join().replace(/,/g, '');
return newVal;
},
parseValue: function (value) {
if (value) {
return value.toString().split('');
}
return [];
},
validate: function (event, inst) {
var disabled = [],
invalid = false;
if (inst.isVisible()) {
inst._markup[0].querySelector('.mbsc-np-dsp').innerHTML = inst.settings.formatValue(event.values, event.variables, inst) || ' ';
}
return {
invalid: invalid,
disabled: disabled
};
}
};
selectSettings: any = {
group: true,
maxWidth: [40, 260],
width: [40, 235],
placeholder: 'Country'
};
widgetSettings: any = {
theme: ' ',
themeVariant: ' ',
display: 'center',
focusOnClose: false,
buttons: [{
text: 'Log in',
handler: 'set'
}]
};
@ViewChild('regForm')
form: any;
@ViewChild('thanks')
thanksPopup: any;
register() {
this.submitted = true;
if (this.form && this.form.valid && this.thanksPopup) {
this.thanksPopup.instance.show();
}
};
countries = [{
value: null,
disabled: true,
text: "",
group: "A"
}, {
value: "1",
text: "Afganistan",
group: "A"
}, {
value: "2",
text: "Albania",
group: "A"
}, {
value: "3",
text: "Algeria",
group: "A"
}, {
value: "4",
text: "Argentina",
group: "A"
}, {
// Showing partial data. Download full source.
}];
}
<mbsc-form [options]="formSettings">
<form novalidate #regForm="ngForm">
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Sign Up</div>
<mbsc-input name="username" [(ngModel)]="username" #user="ngModel" [error]="user.invalid && submitted" errorMessage="At least 2 characters required" placeholder="Username" minlength="2" required icon="user4"></mbsc-input>
<mbsc-input name="email" [(ngModel)]="email" #mail="ngModel" type="email" placeholder="Email" required icon="ion-ios7-email" [error]="mail.invalid && submitted" errorMessage="Required"></mbsc-input>
<mbsc-input name="password" [(ngModel)]="password" #pass="ngModel" type="password" placeholder="Password" minlength="6" required icon="lock2" data-password-toggle="true" [error]="pass.invalid && submitted" errorMessage="At least 6 characters required"></mbsc-input>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Personal data</div>
<mbsc-input name="firstname" [(ngModel)]="firstname" #fname="ngModel" placeholder="First name" required icon="user4" [error]="fname.invalid && submitted" errorMessage="Required"></mbsc-input>
<mbsc-input name="lastname" [(ngModel)]="lastname" #lname="ngModel" placeholder="Last name" required icon="empty" [error]="lname.invalid && submitted" errorMessage="Required"></mbsc-input>
<mbsc-input name="birthday" [(ngModel)]="birthday" #birth="ngModel" mbsc-date [mbsc-options]="dateSettings" placeholder="Birthday" required icon="calendar" [error]="birth.invalid && submitted" errorMessage="Required"></mbsc-input>
<mbsc-input name="phone" [(ngModel)]="phone" #phoneNumber="ngModel" class="md-phone" type="tel" placeholder="Phone" required icon="phone" mbsc-numpad [mbsc-options]="numpadSettings" [error]="phoneNumber.invalid && submitted" errorMessage="Required"></mbsc-input>
<mbsc-input name="country" icon="location" [(ngModel)]="country" #count="ngModel" mbsc-select [mbsc-data]="countries" [mbsc-options]="selectSettings" required [error]="count.invalid && submitted" errorMessage="Required"></mbsc-input>
<mbsc-input name="address" [(ngModel)]="address" placeholder="Address" icon="empty"></mbsc-input>
<mbsc-input name="company" [(ngModel)]="company" placeholder="Company" icon="office"></mbsc-input>
<mbsc-input name="title" [(ngModel)]="title" placeholder="Title" icon="empty"></mbsc-input>
<mbsc-textarea name="about" [(ngModel)]="about" placeholder="About me" icon="line-note"></mbsc-textarea>
<mbsc-input name="site" [(ngModel)]="site" placeholder="Website" icon="globe"></mbsc-input>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Gender</div>
<mbsc-radio-group [(ngModel)]="gender" name="gender">
<mbsc-radio value="male">Male</mbsc-radio>
<mbsc-radio value="female">Female</mbsc-radio>
</mbsc-radio-group>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Settings</div>
<mbsc-switch [(ngModel)]="sendEmail" name="sendEmail">Send me marketing e-mail</mbsc-switch>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">E-mail Categories</div>
<mbsc-checkbox [(ngModel)]="baby" name="email_baby">Baby</mbsc-checkbox>
<mbsc-checkbox [(ngModel)]="beauty" name="email_beauty">Beauty</mbsc-checkbox>
<mbsc-checkbox [(ngModel)]="books" name="email_books">Books</mbsc-checkbox>
<mbsc-checkbox [(ngModel)]="cloths" name="email_cloths">Clothing & Accessories</mbsc-checkbox>
<mbsc-checkbox [(ngModel)]="computer" name="email_computer">Computer & Accessories</mbsc-checkbox>
<mbsc-checkbox [(ngModel)]="electronics" name="email_electronics">Electronics</mbsc-checkbox>
</div>
<div class="mbsc-form-group mbsc-btn-group-block">
<mbsc-button icon="checkmark" (click)="register()" [disabled]="regForm.invalid && submitted">Sign Up</mbsc-button>
</div>
</form>
</mbsc-form>
<mbsc-widget [options]="widgetSettings" #thanks="mobiscroll">
<h3>Thank you for registering</h3>
<p>You have successfully signed up as a user!</p>
</mbsc-widget>
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HttpClientJsonpModule } from '@angular/common/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MbscModule } from '@mobiscroll/angular';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
MbscModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
HttpClientJsonpModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
Forms - Create account
Forms - Sign up
Forms - Log in
Looking for something you didn't see or have a sales question?
Ask us about it, we're here to help.
What framework are you using?
Introducing the next generation of Mobiscroll
The Mobiscroll 5 Beta 4 includes the brand new Scheduler, Agenda and Event calendar, Date, time, calendar and Range picker components.
We’ll be gradually adding more components and functionality. If you want to give the new version a go, you can check them out below.
- Redesigned event calendar, scheduler with drag & drop
- A new way of working with recurring events
- Updated themes with dynamic dark mode
- New date & time pickers
- Range picker with advanced customization
- Dynamic options with global updates
We have to set you up with a trial for this to run 👍
Step 1.Install the Mobiscroll CLI from npm
$ npm install -g @mobiscroll/cli
The CLI makes configuring your apps super simple 👍
Step 2.Run the following command in the root folder of your Ionic project
$ mobiscroll config ionic
You will be prompted to log in with your mobiscroll account. Set your password here
Create an Ionic 3 & Mobiscroll starter with the CLI:
Run this command for Ionic 4 & Mobiscroll starter:
Step 3.Copy the code into your app.
Step 4.Run ionic serve in the root folder of your app 🎉
$ ionic serve
And voilà, everything should be running smoothly.
Step 1.Install the Mobiscroll CLI from npm
$ npm install -g @mobiscroll/cli
The CLI makes configuring your apps super simple 👍
Step 2.Run the following command in the root folder of your Ionic project
$ mobiscroll config ionic
You will be prompted to log in with your mobiscroll account. Set your password here
Run this command for Ionic 5 & React Mobiscroll starter:
Step 4.Run ionic serve in the root folder of your app 🎉
$ ionic serve
And voilà, everything should be running smoothly.
Step 1.Install the Mobiscroll CLI from npm
$ npm install -g @mobiscroll/cli
The CLI makes configuring your apps super simple 👍
Step 2.Run the following command in the root folder of your Angular project
$ mobiscroll config angular
You will be prompted to log in with your mobiscroll account. Set your password here
Step 3.Copy the code into your app. HTML goes into the markup, TS into Typescript.
Step 4.Run ng serve in the root folder of your app 🎉
$ ng serve
And voilà, everything should be running smoothly.
Extract the zip file and run the project like any Ionic app. Make sure to have Ionic CLI installed and open the terminal in the app root folder.
$ npm install
$ ionic serve
Let us know if we can help and enjoy!
Enter a new password and hit the button below to change it!
You are setting a password for the account
Your password has been changed!
Everything is set up so that you can dig in right away and start exploring.
Please extract the zip file and simply open the demos in your favorite browser.
For instructions on how to install Mobiscroll in your project
follow this guide.
Let us know if we can help and enjoy! 👍
Please extract the zip file and run the project like any Angular CLI app.
Make sure to have the Angular CLI installed.
For installation and usage, extract the zip file and open a terminal window and follow these steps.
$ npm install
$ ng serve --open
Let us know if we can help and enjoy! 👍
Everything is set up so that you can dig in right away and start exploring.
We have set up a trial so that you can try the demos locally.
The easiest way to get started is to follow the installation steps and by
grabbing the code directly from the demo page. Let us know if we can help and enjoy! 👍
You'll find a fully functional Kitchen-sink Ionic app in the zip file.
The demos are using Babel's in-browser ES6 and JSX transformer.
Please extract the zip file and open the demos in your browser.
For instructions on how to install Mobiscroll in your project
follow this guide.
Let us know if we can help and enjoy!