英文:
formbuilder angular 9 - problem with date validator
问题
以下是您要翻译的内容:
"I have a task to divided 2 message for datetime input:
- Date is require
- Out of date (like 30/02/2023 is not a valid date)
But the problem is when I use validator with formbuilder. The date return is validate before I click on the button submit. So when I click on the submit button the date always return null if it not valid (it mean I can not set 2 different condition because 2 condition is the same null value). So I can not setError for 2 message control in formbuilder when click on submit button.
Library
import * as dateFormat from "dateformat";
.ts
updateForm: FormGroup;
constructor(
private fb: FormBuilder) { }
createForm() {
let now = new Date();
this.updateForm = this.fb.group({
date: [
dateFormat(now, "yyyy-mm-dd"),
[Validators.required],
],
});
}
ngOnInit(): void {
this.createForm()
}
onSubmit() {
console.log(this.updateForm.value.date)
//always null if date not valid
}
.html
<input type="date" class="form-control input is-primary" formControlName="ngayApDung"
[ngClass]="{ 'is-invalid': submitted && f.date.errors }" cdkFocusInitial />
<div *ngIf="submitted && f.date.errors" class="help is-danger">
<div *ngIf="f.date.errors.required" translate>
date is required
</div>
</div>
英文:
I have a task to divided 2 message for datetime input:
- Date is require
- Out of date (like 30/02/2023 is not a valid date)
But the problem is when I use validator with formbuilder. The date return is validate before I click on the button submit. So when I click on the submit button the date always return null if it not valid (it mean I can not set 2 different condition because 2 condition is the same null value). So I can not setError for 2 message control in formbuilder when click on submit button.
Library
import * as dateFormat from "dateformat";
.ts
updateForm: FormGroup;
constructor(
private fb: FormBuilder) { }
createForm() {
let now = new Date();
this.updateForm = this.fb.group({
date: [
dateFormat(now, "yyyy-mm-dd"),
[Validators.required],
],
});
}
ngOnInit(): void {
this.createForm()
}
onSubmit() {
console.log(this.updateForm.value.date)
//alway null if date not valid
}
.html
<input type="date" class="form-control input is-primary" formControlName="ngayApDung"
[ngClass]="{ 'is-invalid': submitted && f.date.errors }" cdkFocusInitial />
<div *ngIf="submitted && f.date.errors" class="help is-danger">
<div *ngIf="f.date.errors.required" translate>
date is required
</div>
</div>
答案1
得分: 0
HTML 输入元素具有 validityState
类,其中包含许多属性。
当您输入错误的日期时,badInput
属性的值为 true
。
因此,您可以通过添加额外的辅助函数来实现您想要的功能:
首先添加一个新的辅助变量
badInput: boolean = false;
表单仅包含所需的验证
let now = new Date();
this.updateForm = new FormGroup({
date_new: new FormControl(now, [
Validators.required,
]),
});
添加一个新的辅助函数
check(e: any){
const target = e.target as HTMLInputElement;
if (target.validity.badInput){
this.badInput = target.validity.badInput
}
else{
this.badInput = false
}
}
HTML
<input
type="date"
class="form-control input is-primary"
formControlName="date_new"
[(ngModel)]="now"
cdkFocusInitial
formtarget="date : 'yyyy-MM-dd'"
(keyup)="check($event)"
/>
<div *ngIf="updateForm.controls['date_new'].errors && !badInput">请输入日期
</div>
<div *ngIf="updateForm.controls['date_new'].errors && badInput">无效的日期
</div>
您需要使用 updateForm.controls['date_new'].errors
来仅在出现错误时显示 <div>
,而 badInput
会在 HTMLInputElement
的更改后发生变化。
英文:
The HTML input element
has a validityState
class with many attributes.
The badInput
attribute is true
when you put a wrong date.
So you can achieve what you want by adding an extra helper function:
Firstly add a new helper variable
badInput: boolean = false;
Form has only the required Validation
let now = new Date();
this.updateForm = new FormGroup({
date_new: new FormControl(now, [
Validators.required,
]),
});
add a new helper function
check(e: any){
const target = e.target as HTMLInputElement;
if (target.validity.badInput){
this.badInput = target.validity.badInput
}
else{
this.badInput = false
}
}
html
<input
type="date"
class="form-control input is-primary"
formControlName="date_new"
[(ngModel)]="now"
cdkFocusInitial
formtarget="date : 'yyyy-MM-dd'"
(keyup)="check($event)"
/>
<div *ngIf="updateForm.controls['date_new'].errors && !badInput">Please enter a date
</div>
<div *ngIf="updateForm.controls['date_new'].errors && badInput">Invalid date
</div>
You need updateForm.controls['date_new'].errors
to display divs only on errors and the badInput
changes after the change of the HTMLInputElement
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论