Requiring a checkbox to be checked

后端 未结 11 1870
猫巷女王i
猫巷女王i 2020-12-25 09:51

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

相关标签:
11条回答
  • 2020-12-25 10:21

    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>
    
    0 讨论(0)
  • 2020-12-25 10:24
    <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() {
    
      }
    }
    
    0 讨论(0)
  • 2020-12-25 10:25

    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>`
    })
    
    0 讨论(0)
  • 2020-12-25 10:26

    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')]
        });
    
    0 讨论(0)
  • 2020-12-25 10:26

    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>
    
    0 讨论(0)
提交回复
热议问题