Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
angular-training
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Deep Bhuller
angular-training
Commits
b0a53606
Commit
b0a53606
authored
Apr 21, 2021
by
dbhuller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added contact form and account form to application, along with form validation
parent
f6967d65
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
220 additions
and
9 deletions
+220
-9
account.component.css
...sum-angular-handson/src/app/account/account.component.css
+6
-0
account.component.html
...um-angular-handson/src/app/account/account.component.html
+61
-1
account.component.ts
...isum-angular-handson/src/app/account/account.component.ts
+36
-3
app-routing.module.ts
...ndson/nisum-angular-handson/src/app/app-routing.module.ts
+4
-1
app.module.ts
...gular_handson/nisum-angular-handson/src/app/app.module.ts
+9
-2
contact.component.css
...sum-angular-handson/src/app/contact/contact.component.css
+0
-0
contact.component.html
...um-angular-handson/src/app/contact/contact.component.html
+27
-0
contact.component.spec.ts
...angular-handson/src/app/contact/contact.component.spec.ts
+25
-0
contact.component.ts
...isum-angular-handson/src/app/contact/contact.component.ts
+50
-0
footer.component.html
...isum-angular-handson/src/app/footer/footer.component.html
+1
-1
header.component.html
...isum-angular-handson/src/app/header/header.component.html
+1
-1
No files found.
nisum_angular_handson/nisum-angular-handson/src/app/account/account.component.css
View file @
b0a53606
.invalid-text
{
color
:
red
;
}
.ng-invalid.ng-touched
:not
(
form
)
{
border
:
2px
solid
red
;
}
\ No newline at end of file
nisum_angular_handson/nisum-angular-handson/src/app/account/account.component.html
View file @
b0a53606
<p>
account works!
</p>
<div>
<div
style=
"display:inline-block"
>
<form
[
formGroup
]="
userForm
"
(
ngSubmit
)="
onSubmit1
()"
>
<div>
<p>
reactive
</p>
<div>
<input
type=
"text"
formControlName=
"firstName"
class=
"form-control input"
placeholder=
"First name"
>
</div>
<div>
<input
type=
"text"
formControlName=
"lastName"
class=
"form-control input "
placeholder=
"Last name"
>
</div>
</div>
<input
type=
"email"
formControlName=
"email"
class=
"form-control input"
placeholder=
"E-mail"
>
<div>
<button
type=
"submit"
>
Sign in
</button>
</div>
</form>
<div>
<ngb-alert
*
ngIf=
"showMessage"
type=
"danger"
>
{{ invalidEntry }}
</ngb-alert>
<!-- <div class="invalid-text" *ngIf="showMessage">
{{invalidEntry}}
</div> -->
</div>
</div>
<!-- template driven form -->
<div
style=
"display:inline-block; margin-left: 10em;"
>
<form
#
newUserForm=
"ngForm"
(
ngSubmit
)="
onSubmit2
(
newUserForm
)"
>
<div>
<p>
template-driven
</p>
<input
type=
"text"
class=
"input"
placeholder=
"User name"
required
[(
ngModel
)]="
userName
"
name=
"userName"
pattern=
"[a-zA-Z ]*"
#
pickedName=
"ngModel"
>
<div
*
ngIf=
"!pickedName.valid && pickedName.touched"
>
User name is required!
</div>
</div>
<div>
<input
type=
"email"
class=
"input"
placeholder=
"Email"
required
[(
ngModel
)]="
email
"
name=
"email"
#
userEmail=
"ngModel"
pattern=
"[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}"
>
<div
*
ngIf=
"!userEmail.valid && userEmail.touched"
>
Email is not valid!
</div>
</div>
<div>
<input
type=
"password"
class=
"input"
placeholder=
"Password"
required
[(
ngModel
)]="
password
"
name=
"password"
#
userPassword=
"ngModel"
>
<div
*
ngIf=
"!userPassword.valid && userPassword.touched"
>
Password is required!
</div>
</div>
<button
type=
"submit"
[
disabled
]="!
newUserForm
.
form
.
valid
"
>
Register
</button>
<button
type=
"button"
(
click
)="
newUserForm
.
reset
()"
>
Reset
</button>
</form>
<!-- <pre>{{ newUserForm.form.value }}</pre> Value of whole form -->
<!-- <pre>User name: {{ pickedName.value }}</pre> Value of userName field -->
<pre>
Valid form? {{ newUserForm.form.valid }}
</pre>
</div>
</div>
\ No newline at end of file
nisum_angular_handson/nisum-angular-handson/src/app/account/account.component.ts
View file @
b0a53606
import
{
Component
,
OnInit
}
from
'@angular/core'
;
import
{
Component
,
OnInit
}
from
'@angular/core'
;
import
{
FormBuilder
,
Validators
}
from
'@angular/forms'
;
import
{
NgForm
}
from
'@angular/forms'
;
@
Component
({
@
Component
({
selector
:
'app-account'
,
selector
:
'app-account'
,
templateUrl
:
'./account.component.html'
,
templateUrl
:
'./account.component.html'
,
styleUrls
:
[
'./account.component.css'
]
styleUrls
:
[
'./account.component.css'
]
})
})
export
class
AccountComponent
implements
OnInit
{
constructor
()
{
}
export
class
AccountComponent
implements
OnInit
{
public
userForm
;
public
showMessage
:
boolean
=
false
;
public
invalidEntry
=
'Invalid entry'
;
//template driven form
userName
=
''
;
email
:
string
;
nickName
:
string
;
password
:
string
;
constructor
(
private
formBuilder
:
FormBuilder
)
{
}
ngOnInit
():
void
{
ngOnInit
():
void
{
this
.
userForm
=
this
.
formBuilder
.
group
({
firstName
:
[
''
,
[
Validators
.
required
,
Validators
.
pattern
(
'^[a-zA-Z]+$'
)]],
lastName
:
[
''
,[
Validators
.
required
,
Validators
.
pattern
(
'^[a-zA-Z]+$'
)]],
email
:
[
''
,
[
Validators
.
required
,
Validators
.
pattern
(
'[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}'
)]]
});
}
onSubmit
(){
if
(
this
.
userForm
.
valid
){
alert
(
'User form is valid!!'
)
}
else
{
alert
(
'User form is not valid!!'
)
}
}
onSubmit1
(){
if
(
!
this
.
userForm
.
valid
){
this
.
showMessage
=
true
;
}
}
//template driven
onSubmit2
(
form
:
NgForm
)
{
if
(
form
.
valid
)
{
console
.
log
(
form
.
value
);
// ...our form is valid, we can submit the data
}
}
}
}
}
nisum_angular_handson/nisum-angular-handson/src/app/app-routing.module.ts
View file @
b0a53606
...
@@ -5,6 +5,7 @@ import { FranceComponent } from './france/france.component';
...
@@ -5,6 +5,7 @@ import { FranceComponent } from './france/france.component';
import
{
GreeceComponent
}
from
'./greece/greece.component'
;
import
{
GreeceComponent
}
from
'./greece/greece.component'
;
import
{
SpainComponent
}
from
'./spain/spain.component'
;
import
{
SpainComponent
}
from
'./spain/spain.component'
;
import
{
AccountComponent
}
from
'./account/account.component'
;
import
{
AccountComponent
}
from
'./account/account.component'
;
import
{
ContactComponent
}
from
'./contact/contact.component'
;
const
routes
:
Routes
=
[
const
routes
:
Routes
=
[
...
@@ -12,7 +13,9 @@ const routes: Routes = [
...
@@ -12,7 +13,9 @@ const routes: Routes = [
{
path
:
'france'
,
component
:
FranceComponent
},
{
path
:
'france'
,
component
:
FranceComponent
},
{
path
:
'account'
,
component
:
AccountComponent
},
{
path
:
'account'
,
component
:
AccountComponent
},
{
path
:
'greece'
,
component
:
GreeceComponent
},
{
path
:
'greece'
,
component
:
GreeceComponent
},
{
path
:
'spain'
,
component
:
SpainComponent
}];
{
path
:
'spain'
,
component
:
SpainComponent
},
{
path
:
'contact'
,
component
:
ContactComponent
}
];
@
NgModule
({
@
NgModule
({
imports
:
[
RouterModule
.
forRoot
(
routes
)],
imports
:
[
RouterModule
.
forRoot
(
routes
)],
...
...
nisum_angular_handson/nisum-angular-handson/src/app/app.module.ts
View file @
b0a53606
...
@@ -3,6 +3,9 @@ import { BrowserModule } from '@angular/platform-browser';
...
@@ -3,6 +3,9 @@ import { BrowserModule } from '@angular/platform-browser';
import
{
NgbModule
}
from
'@ng-bootstrap/ng-bootstrap'
import
{
NgbModule
}
from
'@ng-bootstrap/ng-bootstrap'
import
{
AppRoutingModule
}
from
'./app-routing.module'
;
import
{
AppRoutingModule
}
from
'./app-routing.module'
;
import
{
FormsModule
}
from
'@angular/forms'
;
import
{
NgForm
}
from
'@angular/forms'
;
import
{
ReactiveFormsModule
}
from
'@angular/forms'
;
import
{
AppComponent
}
from
'./app.component'
;
import
{
AppComponent
}
from
'./app.component'
;
import
{
HeaderComponent
}
from
'./header/header.component'
;
import
{
HeaderComponent
}
from
'./header/header.component'
;
import
{
FooterComponent
}
from
'./footer/footer.component'
;
import
{
FooterComponent
}
from
'./footer/footer.component'
;
...
@@ -11,6 +14,7 @@ import { FranceComponent } from './france/france.component';
...
@@ -11,6 +14,7 @@ import { FranceComponent } from './france/france.component';
import
{
SpainComponent
}
from
'./spain/spain.component'
;
import
{
SpainComponent
}
from
'./spain/spain.component'
;
import
{
GreeceComponent
}
from
'./greece/greece.component'
;
import
{
GreeceComponent
}
from
'./greece/greece.component'
;
import
{
AccountComponent
}
from
'./account/account.component'
;
import
{
AccountComponent
}
from
'./account/account.component'
;
import
{
ContactComponent
}
from
'./contact/contact.component'
;
@
NgModule
({
@
NgModule
({
declarations
:
[
declarations
:
[
...
@@ -21,12 +25,15 @@ import { AccountComponent } from './account/account.component';
...
@@ -21,12 +25,15 @@ import { AccountComponent } from './account/account.component';
FranceComponent
,
FranceComponent
,
SpainComponent
,
SpainComponent
,
GreeceComponent
,
GreeceComponent
,
AccountComponent
AccountComponent
,
ContactComponent
],
],
imports
:
[
imports
:
[
BrowserModule
,
BrowserModule
,
AppRoutingModule
,
AppRoutingModule
,
NgbModule
NgbModule
,
FormsModule
,
ReactiveFormsModule
],
],
providers
:
[],
providers
:
[],
bootstrap
:
[
AppComponent
]
bootstrap
:
[
AppComponent
]
...
...
nisum_angular_handson/nisum-angular-handson/src/app/contact/contact.component.css
0 → 100644
View file @
b0a53606
nisum_angular_handson/nisum-angular-handson/src/app/contact/contact.component.html
0 → 100644
View file @
b0a53606
<div
style=
"display:inline-block"
>
<form
[
formGroup
]="
contactForm
"
(
ngSubmit
)="
onSubmit
()"
>
<div>
<p>
Contact Us!
</p>
<div>
<input
type=
"text"
formControlName=
"firstName"
required
class=
"form-control input"
placeholder=
"First name"
>
<!-- <div *ngIf="errors.required">First Name Required</div> -->
</div>
<div>
<input
type=
"text"
formControlName=
"lastName"
required
class=
"form-control input "
placeholder=
"Last name"
>
</div>
</div>
<input
type=
"email"
formControlName=
"email"
required
class=
"form-control input"
placeholder=
"E-mail"
>
<textarea
type=
"text"
formControlName=
"message"
class=
"form-control input"
placeholder=
"Type Message Here..."
></textarea>
<div>
<button
type=
"submit"
>
Send
</button>
</div>
</form>
<div>
<ngb-alert
*
ngIf=
"formValid == false"
type=
"danger"
>
{{ msg }}
</ngb-alert>
<ngb-alert
*
ngIf=
"formValid == true"
type=
"success"
>
{{ msg }}
</ngb-alert>
<div
class=
"invalid-text"
*
ngIf=
"formValid"
>
{{ msg }}
</div>
</div>
</div>
\ No newline at end of file
nisum_angular_handson/nisum-angular-handson/src/app/contact/contact.component.spec.ts
0 → 100644
View file @
b0a53606
import
{
ComponentFixture
,
TestBed
}
from
'@angular/core/testing'
;
import
{
ContactComponent
}
from
'./contact.component'
;
describe
(
'ContactComponent'
,
()
=>
{
let
component
:
ContactComponent
;
let
fixture
:
ComponentFixture
<
ContactComponent
>
;
beforeEach
(
async
()
=>
{
await
TestBed
.
configureTestingModule
({
declarations
:
[
ContactComponent
]
})
.
compileComponents
();
});
beforeEach
(()
=>
{
fixture
=
TestBed
.
createComponent
(
ContactComponent
);
component
=
fixture
.
componentInstance
;
fixture
.
detectChanges
();
});
it
(
'should create'
,
()
=>
{
expect
(
component
).
toBeTruthy
();
});
});
nisum_angular_handson/nisum-angular-handson/src/app/contact/contact.component.ts
0 → 100644
View file @
b0a53606
import
{
Component
,
OnInit
}
from
'@angular/core'
;
import
{
FormBuilder
,
Validators
}
from
'@angular/forms'
;
// import { NgForm } from '@angular/forms';
@
Component
({
selector
:
'contact'
,
templateUrl
:
'./contact.component.html'
,
styleUrls
:
[
'./contact.component.css'
]
})
export
class
ContactComponent
implements
OnInit
{
public
contactForm
;
public
formValid
:
boolean
;
public
formInvalid
:
boolean
;
public
msg
:
string
=
""
;
constructor
(
private
formBuilder
:
FormBuilder
)
{
}
ngOnInit
():
void
{
this
.
contactForm
=
this
.
formBuilder
.
group
({
firstName
:
[
''
,
[
Validators
.
required
,
Validators
.
pattern
(
'^[a-zA-Z]+$'
)]],
lastName
:
[
''
,[
Validators
.
required
,
Validators
.
pattern
(
'^[a-zA-Z]+$'
)]],
email
:
[
''
,
[
Validators
.
required
,
Validators
.
pattern
(
'[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}'
)]],
message
:
[
''
]
});
}
// onSubmit() {
// if(this.contactForm.valid) {
// alert('Contact form sent!');
// } else {
// alert('Form is not valid');
// }
// }
onSubmit
()
{
this
.
formValid
=
this
.
contactForm
.
valid
;
this
.
formInvalid
=
this
.
contactForm
.
invalid
;
if
(
this
.
formInvalid
)
{
this
.
msg
=
"Contact form invalid"
;
}
else
{
this
.
msg
=
"Contact form Sent"
;
}
console
.
log
(
"valid: "
+
this
.
formValid
);
console
.
log
(
"invalid: "
+
this
.
formInvalid
);
console
.
log
(
this
.
contactForm
);
}
}
nisum_angular_handson/nisum-angular-handson/src/app/footer/footer.component.html
View file @
b0a53606
...
@@ -2,6 +2,6 @@
...
@@ -2,6 +2,6 @@
<ul>
<ul>
<li>
About Us
</li>
<li>
About Us
</li>
<li>
Careers
</li>
<li>
Careers
</li>
<
li>
Contact Us
</li
>
<
a
[
routerLink
]="['/
contact
']"
><li>
Contact Us
</li></a
>
</ul>
</ul>
</div>
</div>
nisum_angular_handson/nisum-angular-handson/src/app/header/header.component.html
View file @
b0a53606
...
@@ -6,6 +6,6 @@
...
@@ -6,6 +6,6 @@
<button
[
routerLink
]="['/
greece
']"
>
Greece
</button>
<button
[
routerLink
]="['/
greece
']"
>
Greece
</button>
</div>
</div>
<span
class=
"account"
>
<span
class=
"account"
>
<i
class=
"fa fa-user"
></i>
<i
class=
"fa fa-user"
[
routerLink
]="['/
account
']"
></i>
</span>
</span>
</div>
</div>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment