Last active
February 22, 2019 12:59
-
-
Save Danielkaas94/d11271d0081b5a023e6bebcbe08b00fc to your computer and use it in GitHub Desktop.
Forms & Validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, Input } from '@angular/core'; | |
import { Validators, FormBuilder, FormControl, FormGroup } from "@angular/forms"; | |
@Component({ | |
selector: 'app-form', | |
templateUrl: './form.component.html', | |
styleUrls: ['./form.component.css'] | |
}) | |
export class FormComponent implements OnInit { | |
form: FormGroup; | |
ngOnInit() { | |
this.form = new FormGroup({ | |
medium: new FormControl('Movies'), | |
name: new FormControl('', Validators.compose([ | |
Validators.required, | |
Validators.pattern('[\\w\\-\\s\\/]+') | |
])), | |
category: new FormControl(''), | |
year: new FormControl(''), | |
text: new FormControl('', this.yearValidator), | |
}); | |
} | |
yearValidator(control) { | |
if (control.value.trim().length === 0) { | |
return null; | |
} | |
let year = parseInt(control.value); | |
let minYear = 1900; | |
let maxYear = 2100; | |
if (year >= minYear && year <= maxYear) { | |
return null; | |
} else { | |
return { 'year': true } | |
} | |
} | |
onSubmit(mediaItem) { | |
console.log(mediaItem); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, Input } from '@angular/core'; | |
import { Validators, FormBuilder, FormControl, FormGroup } from "@angular/forms"; | |
import { MediaItemService } from "../../media-item.service"; | |
@Component({ | |
selector: 'app-form', | |
templateUrl: './form.component.html', | |
styleUrls: ['./form.component.css'] | |
}) | |
export class FormComponent implements OnInit { | |
formGroup: FormGroup; | |
constructor(private formBuilder: FormBuilder, private mediaItemService: MediaItemService) { } | |
ngOnInit() { | |
this.formGroup = this.formBuilder.group({ | |
medium: this.formBuilder.control('Movies'), | |
name: this.formBuilder.control('', Validators.compose([ | |
Validators.required, | |
Validators.pattern('[\\w\\-\\s\\/]+') | |
])), | |
category: this.formBuilder.control(''), | |
year: this.formBuilder.control(''), | |
text: this.formBuilder.control('', this.yearValidator), | |
}) | |
} | |
onSubmit(mediaItem) { | |
this.mediaItemService.add(mediaItem); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class MediaItemService { | |
get() { | |
return this.mediaItems; | |
} | |
add(mediaItem) { | |
this.mediaItems.push(mediaItem); | |
} | |
delete(mediaItem) { | |
let index = this.mediaItems.indexOf(mediaItem) | |
if (index >= 0) { | |
this.mediaItems.splice(index, 1) | |
} | |
} | |
mediaItems = [ | |
{ | |
id: 1, | |
name: "", | |
medium: "", | |
category: "", | |
year: 2018, | |
watchedOn: null, | |
isFavorite: false | |
}, | |
{ | |
id: 2, | |
name: "", | |
medium: "", | |
category: "", | |
year: 2012, | |
watchedOn: null, | |
isFavorite: false | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment