简体   繁体   中英

Validation dependency between inputs in angular2 form?

I'm trying to find the best way to have validations that link two components.

Say I have two checkboxes. I want the second input to be required only when the first one is checked.

I'm just starting out with validations and am not sure exactly how I could do this.

I guess I could just change the validation rules in my component.ts file when using model driven forms.

Would that be the good way of dealing with this? Or are there helpers to handle such cases?

declare a variable named checbox1checked, and assign an initial value of false to it.

let checbox1checked: boolean = false;

if a user checked checkbox1, assign checbox1checked the value of true.

while validating checkbox2 check to see if checbox1checked is true, if yes then show your error message for checkbox2, if not ignore your checkbox2 error message.

May be you are looking for this:

<input type="checkbox2" [attr.required]="checked1">
<input type="checkbox1" [(ngModel)]="checked1">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM