I want a button to be disabled until a checkbox has been checked using a FormBuilder for Angular. I don\'t want to explicitly check the value of the checkbox and would prefe
If you are using PrimeNG, you can do it thru a TAG app-form-required-field, like this:
<p-checkbox name="_yes" #active="ngModel" required value="true"
label="Active" binary="true" [(ngModel)]="filter._yes"></p-checkbox>
<p-checkbox name="_no" #inactive="ngModel" required label="Inactive"
binary="true" [(ngModel)]="filter._no"></p-checkbox>
<app-form-required-field
*ngIf="!filter._yes && !filter._no"
[form]="active"
[form]="inactive"
id="msgAtivo"
requiredMessage="Field required!"
>
</app-form-required-field>
<h1>LOGIN</h1>
<form [formGroup]="signUpForm">
<input type="checkbox" formControlName="cb">
<button type="submit" [disabled]="!loginForm.valid" (click)="doLogin()">Log in</button>
</form>
export class Login {
public signUpForm: FormGroup;
constructor(fb: FormBuilder) {
this.signUpForm = fb.group({
cb: [false, Validators.requiredTrue]
});
}
doLogin() {
}
}
despite of putting validator simple check below conditions
@Component({
selector: 'my-form',
directives: [FORM_DIRECTIVES],
template: ` <form [ngFormModel]="form" (ngSubmit)="onSubmit(form.value)">
<input type="checkbox" id="cb" ngControl="cb">
<button type="submit" [disabled]="!form.valid && !cb.value">
</form>`
})
You could just use a ValidatorPattern and check for the right (boolean) value:
<input type="checkbox" [formControl]="myForm.controls['isTosRead']">
and here is the binding:
this.myForm = builder.group({
isTosRead: [false, Validators.pattern('true')]
});
I have this really simple example:
In your component:
login : FormGroup;
constructor(@Inject(FormBuilder)formBuilder : FormBuilder) {
this.login = formBuilder.group({userName: [null], password: [null],
staySignedIn: [false,Validators.pattern('true')]});
}
In your HTML:
<form [formGroup]="login" (ngSubmit)="onSubmit()">
<div class="form-group">
<input formControlName="userName" required>
</div>
<div class="form-group">
<input formControlName="password" type="password" required>
</div>
<div>
<label>
<input formControlName="staySignedIn" checked="staySignedIn" type="checkbox"> bla
</label>
</div>
<button type="submit">bla</button>
<div >
<a href>bla?</a>
</div>
</form>