The Checkbox component provides a simple boolean toggle for yes/no choices, feature toggles, and user agreements. It’s ideal for single options that can be independently toggled.
import { Validators } from '@angular/forms';const termsCheckbox = new Checkbox( 'acceptTerms', 'I agree to the terms and conditions', false // initial value).apply({ validator: Validators.requiredTrue, errorMessage: 'You must accept the terms to continue', singleLine: true});
Use Validators.requiredTrue (not Validators.required) to ensure the checkbox must be checked, not just interacted with.
const newsletterCheckbox = new Checkbox( 'newsletter', 'Subscribe to our newsletter', true // pre-checked).apply({ icon: 'mail', hint: 'You can unsubscribe at any time'});
const disabledCheckbox = new Checkbox( 'feature', 'Premium feature (requires upgrade)', false).apply({ disabled: true, hint: 'Upgrade to Pro to enable this feature'});
import { Validators } from '@angular/forms';const checkbox = new Checkbox( 'terms', 'I accept the terms').apply({ validator: Validators.requiredTrue, errorMessage: 'You must accept the terms'});
import { Component } from '@angular/core';import { Validators } from '@angular/forms';import { FormStructure, Input, Checkbox, Button } from 'mat-dynamic-form';@Component({ selector: 'app-registration-form', template: '<mat-dynamic-form [structure]="formStructure"></mat-dynamic-form>'})export class RegistrationFormComponent { formStructure: FormStructure; constructor() { this.formStructure = new FormStructure('Create Account'); this.formStructure.appearance = 'outline'; this.formStructure.nodeGrid = 2; this.formStructure.nodes = [ new Input('email', 'Email').apply({ icon: 'email', validator: [Validators.required, Validators.email], singleLine: true }), new Input('password', 'Password').apply({ icon: 'lock', validator: [Validators.required, Validators.minLength(8)], singleLine: true }), // Preferences section new Checkbox('newsletter', 'Send me product updates and newsletters').apply({ icon: 'mail', hint: 'You can unsubscribe at any time', singleLine: true }), new Checkbox('notifications', 'Enable email notifications').apply({ icon: 'notifications', value: true, // Default to enabled singleLine: true }), new Checkbox('shareData', 'Share anonymous usage data to improve the product').apply({ icon: 'analytics', hint: 'Helps us make the product better for everyone', singleLine: true }), // Required terms acceptance new Checkbox( 'terms', 'I agree to the Terms of Service and Privacy Policy' ).apply({ validator: Validators.requiredTrue, errorMessage: 'You must accept the terms to create an account', singleLine: true }), new Checkbox( 'age', 'I confirm that I am at least 18 years old' ).apply({ validator: Validators.requiredTrue, errorMessage: 'You must be at least 18 years old', singleLine: true }) ]; this.formStructure.validateActions = [ new Button('submit', 'Create Account', { style: 'primary', onEvent: (param) => this.onSubmit(param.structure) }).apply({ validateForm: true, icon: 'person_add' }) ]; } onSubmit(structure: FormStructure) { const data = structure.getValue(); console.log('Registration data:', data); // data.newsletter, data.notifications, etc. will be boolean values }}