admin管理员组文章数量:1315982
Is there a way to create a reactive form basing on an existing data model with all the validation magic. In the example below author passes whole new object into a formbuilder
but what I want to achieve is elegant way to tell formbuilder
what field is required or needs some other validation.
export class PersonalData {
email: string = '';
mobile: string = '';
country: string = '';
}
...
createFormGroupWithBuilderAndModel(formBuilder: FormBuilder) {
return formBuilder.group({
personalData: formBuilder.group(new PersonalData()),
requestType: '',
text: ''
});
}
I just want to skip the process of assigning a FormControl
for each field in the model.
@EDIT
After some research and little hint from @xrobert35 I wanted to try and used /@rxweb/reactive-form-validators
Is there a way to create a reactive form basing on an existing data model with all the validation magic. In the example below author passes whole new object into a formbuilder
but what I want to achieve is elegant way to tell formbuilder
what field is required or needs some other validation.
https://malcoded./posts/angular-fundamentals-reactive-forms
export class PersonalData {
email: string = '';
mobile: string = '';
country: string = '';
}
...
createFormGroupWithBuilderAndModel(formBuilder: FormBuilder) {
return formBuilder.group({
personalData: formBuilder.group(new PersonalData()),
requestType: '',
text: ''
});
}
I just want to skip the process of assigning a FormControl
for each field in the model.
@EDIT
After some research and little hint from @xrobert35 I wanted to try and used https://www.npmjs./package/@rxweb/reactive-form-validators
Share Improve this question edited Jul 22, 2021 at 18:44 Alireza Ahmadi 9,9538 gold badges25 silver badges61 bronze badges asked Sep 21, 2018 at 11:31 HagalazHagalaz 1094 silver badges14 bronze badges 1- I wish the form was just bindings directly to my model, and the constraints were just made as html-attributes. I have a rather plicated model, and it requires so much boiler-plate to go model-to-form and back form-to-model. For editing an existing model, I'm storing references back to the original entity. Not sure if there's a cleaner way. :/ – Jeppe Commented Aug 17, 2022 at 17:43
3 Answers
Reset to default 6They could be "many" way to do what you want to do, but by just extending your actual solution : Your personnal data could look like :
export class PersonalData {
email: string = ['', Validators.required];
mobile: string = ['', Validators.required, Validators.maxLength(10)];
country: string = '';
}
If you need domain base validation (for reusable purpose) you can use rxweb validations.
export class PersonalData {
@required()
email: string;
@required()
mobile: string;
@greaterThan({ fieldName: 'number2' })
number1: number;
@prop()
number2: number;
}
@Component({
selector: 'app-root',
templateUrl: './app.ponent.html',
styleUrls: ['./app.ponent.css']
})
export class AppComponent implements OnInit {
formGroup: FormGroup;
constructor( private validation: RxFormBuilder) {
}
ngOnInit() {
let person: PersonalData = new PersonalData();
this.formGroup = this.validation.formGroup(person);
}
}
If I understand you just want to add validators to your field.
https://angular.io/guide/form-validation
I can't be more precise to giving you the official documentation.
ngOnInit(): void {
this.heroForm = new FormGroup({
'name': new FormControl(this.hero.name, [
Validators.required,
Validators.minLength(4),
forbiddenNameValidator(/bob/i) // <-- Here's how you pass in the custom validator.
]),
'alterEgo': new FormControl(this.hero.alterEgo),
'power': new FormControl(this.hero.power, Validators.required)
});
}
In this exemple the fields name and power are required and of course the syntaxe could differ but the flow is the same.
Does it helps you ?
@EDIT There is the same post for your use case: https://stackoverflow./a/47576916/7133262
本文标签: javascriptAngular reactive form based on model with validationStack Overflow
版权声明:本文标题:javascript - Angular reactive form based on model with validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741994625a2409793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论