Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
awa-w8d2-angularcont-practice
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
Eric Arndt
awa-w8d2-angularcont-practice
Commits
bcfd7b2e
Commit
bcfd7b2e
authored
Apr 21, 2021
by
earndt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[W8D3] (ArndtED) Adds reactive, template account forms
parent
71cbdfba
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
108 additions
and
18 deletions
+108
-18
account.component.css
myApp/src/app/account/account.component.css
+5
-0
account.component.html
myApp/src/app/account/account.component.html
+66
-1
account.component.ts
myApp/src/app/account/account.component.ts
+35
-16
app.module.ts
myApp/src/app/app.module.ts
+2
-1
No files found.
myApp/src/app/account/account.component.css
View file @
bcfd7b2e
...
@@ -6,3 +6,8 @@
...
@@ -6,3 +6,8 @@
.invalid-text
{
.invalid-text
{
color
:
red
;
color
:
red
;
}
}
.ng-invalid.ng-touched
:not
(
form
)
{
border
:
2px
solid
red
;
}
\ No newline at end of file
myApp/src/app/account/account.component.html
View file @
bcfd7b2e
...
@@ -18,10 +18,75 @@
...
@@ -18,10 +18,75 @@
</div>
</div>
</form>
</form>
<div
>
<div
>
<
ngb-alert
*
ngIf=
"showMessage"
type=
"danger"
>
{{ invalidEntry }}
</ngb-alert
>
<
!-- <ngb-alert *ngIf="showMessage" type="danger" >{{ invalidEntry }}</ngb-alert> --
>
<!-- <div class= "invalid-text" *ngIf="showMessage">
<!-- <div class= "invalid-text" *ngIf="showMessage">
{{invalidEntry}}
{{invalidEntry}}
</div> -->
</div> -->
</div>
</div>
</div>
</div>
<div>
<div
style=
"display:inline-block"
>
<form
[
formGroup
]="
userForm
"
(
ngSubmit
)="
onSubmit
()"
>
<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
myApp/src/app/account/account.component.ts
View file @
bcfd7b2e
import
{
Component
,
OnInit
}
from
'@angular/core'
;
import
{
Component
,
OnInit
}
from
'@angular/core'
;
import
{
FormGroup
,
FormControl
,
Validators
,
FormBuilder
}
from
'@angular/forms'
;
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
{
export
class
AccountComponent
implements
OnInit
{
public
userForm
;
userForm
:
FormGroup
;
public
showMessage
:
boolean
=
false
;
showMessage
:
true
;
public
invalidEntry
=
'Invalid entry'
;
//template driven form
constructor
(
fb
:
FormBuilder
)
{
userName
=
''
;
this
.
userForm
=
fb
.
group
({
email
:
string
;
firstName
:
[
""
,
Validators
.
required
],
nickName
:
string
;
lastName
:
[
""
,
Validators
.
required
],
password
:
string
;
email
:
[
""
,
Validators
.
required
],
constructor
(
private
formBuilder
:
FormBuilder
)
{
}
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
(){
onSubmit1
()
{}
if
(
this
.
userForm
.
valid
){
alert
(
'User form is valid!!'
)
ngOnInit
():
void
{
}
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
}
}
}
}
}
\ No newline at end of file
myApp/src/app/app.module.ts
View file @
bcfd7b2e
import
{
NgModule
}
from
'@angular/core'
;
import
{
NgModule
}
from
'@angular/core'
;
import
{
NgbModule
}
from
'@ng-bootstrap/ng-bootstrap'
;
import
{
NgbModule
}
from
'@ng-bootstrap/ng-bootstrap'
;
import
{
BrowserModule
}
from
'@angular/platform-browser'
;
import
{
BrowserModule
}
from
'@angular/platform-browser'
;
import
{
ReactiveFormsModule
}
from
'@angular/forms'
;
import
{
FormsModule
,
ReactiveFormsModule
}
from
'@angular/forms'
;
import
{
AppRoutingModule
}
from
'./app-routing.module'
;
import
{
AppRoutingModule
}
from
'./app-routing.module'
;
import
{
AppComponent
}
from
'./app.component'
;
import
{
AppComponent
}
from
'./app.component'
;
...
@@ -27,6 +27,7 @@ import { AccountComponent } from './account/account.component';
...
@@ -27,6 +27,7 @@ import { AccountComponent } from './account/account.component';
imports
:
[
imports
:
[
BrowserModule
,
BrowserModule
,
AppRoutingModule
,
AppRoutingModule
,
FormsModule
,
ReactiveFormsModule
,
ReactiveFormsModule
,
NgbModule
,
NgbModule
,
],
],
...
...
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