|
|
@@ -5,6 +5,7 @@ import { Observable } from 'rxjs';
|
|
|
import { Article, CategoryName } from '../bo/article';
|
|
|
import { ArticleGroup } from '../bo/article-group';
|
|
|
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
|
|
|
+import { Router } from '@angular/router';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'is-article-form',
|
|
|
@@ -17,11 +18,12 @@ import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators }
|
|
|
]
|
|
|
})
|
|
|
export class ArticleForm implements OnInit {
|
|
|
+ formSubmitted = false;
|
|
|
articleForm = new FormGroup({
|
|
|
title: new FormControl('', Validators.required),
|
|
|
text: new FormControl('', Validators.required),
|
|
|
- categoryName: new FormControl(''),
|
|
|
- articleGroup: new FormControl(''),
|
|
|
+ categoryName: new FormControl('', Validators.required),
|
|
|
+ articleGroup: new FormControl(null, Validators.required),
|
|
|
})
|
|
|
private dataService = inject(DataService)
|
|
|
|
|
|
@@ -29,13 +31,25 @@ export class ArticleForm implements OnInit {
|
|
|
articleGroups$?: Observable<ArticleGroup[]>;
|
|
|
categoryNames: CategoryName[] = Object.values(CategoryName);
|
|
|
|
|
|
+ constructor(private router: Router) {
|
|
|
+ }
|
|
|
+
|
|
|
ngOnInit() {
|
|
|
- const fc = new FormControl('', Validators.required);
|
|
|
this.articleGroups$ = this.dataService.getArticleGroups();
|
|
|
+ this.listenToInputChange("title");
|
|
|
+ this.listenToInputChange("text");
|
|
|
}
|
|
|
|
|
|
handleSubmit(){
|
|
|
+ this.formSubmitted = true;
|
|
|
+
|
|
|
+ this.articleForm.updateValueAndValidity();
|
|
|
console.log(this.articleForm.valid);
|
|
|
+ console.log(this.articleForm.controls.articleGroup.value);
|
|
|
+ console.log(this.articleForm.controls.title.errors);
|
|
|
+ console.log(this.articleForm.controls.text.errors);
|
|
|
+ console.log(this.articleForm.controls.categoryName.errors);
|
|
|
+ console.log(this.articleForm.controls.articleGroup.errors);
|
|
|
if (!this.articleForm.valid) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -44,15 +58,31 @@ export class ArticleForm implements OnInit {
|
|
|
id: 0,
|
|
|
title: this.articleForm.value.title!,
|
|
|
text: this.articleForm.value.text!,
|
|
|
- categoryName: CategoryName.sport,
|
|
|
- articleGroupId: 3
|
|
|
+ categoryName: this.articleForm.value.categoryName! as CategoryName,
|
|
|
+ articleGroupId: this.articleForm.value.articleGroup!
|
|
|
};
|
|
|
|
|
|
this.dataService.postArticle(article).subscribe((article: Article) => {
|
|
|
- console.log(article);
|
|
|
+ console.log(article)
|
|
|
+ this.router.navigateByUrl('/');
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ listenToInputChange(controlName: keyof typeof this.articleForm.controls){
|
|
|
+ console.log(this.articleForm.controls)
|
|
|
+ const control = this.articleForm.controls[controlName];
|
|
|
+ control.valueChanges.subscribe(value => {
|
|
|
+ const regExp = new RegExp('^([A-Za-z]|\\s)*$')
|
|
|
+ const errors = { ...control.errors };
|
|
|
|
|
|
+ if (value && !regExp.test(value)) {
|
|
|
+ errors['invalidString'] = true;
|
|
|
+ } else {
|
|
|
+ delete errors['invalidString'];
|
|
|
+ }
|
|
|
+
|
|
|
+ control.setErrors(Object.values(errors).length === 0 ? null : errors);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
}
|