import { ChangeDetectionStrategy, Component, inject, signal, ViewEncapsulation, } from '@angular/core'; import { EpdfDocumentGate, provideEmbedPdf } from '@embedpdf/angular/runtime'; import type { OpenInput } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, withStage } from '@embedpdf/angular/stage'; import { EpdfRenderLayer, withRender } from '@embedpdf/angular/render'; import { withInteraction } from '@embedpdf/angular/interaction'; import { withActions } from '@embedpdf/angular/actions'; import { EpdfForm, EpdfFormLayer, withForm } from '@embedpdf/angular/form'; import type { FormValuesValidation } from '@embedpdf/angular/form'; import { cloudEngine } from '@cloudpdf/engine'; const invoice: OpenInput = { kind: 'grant', grant: 'shr_NCOaGzAKFsdI2r-UaVUJG7zL' }; // An order as a backend might send it: a quantity that is no number, a date that is no date. const ORDER = { 'billing.name': 'Ada Lovelace', 'invoice.license.qty': 'two', date: 'next week' }; @Component({ selector: 'demo-root', imports: [EpdfDocumentGate, EpdfStage, EpdfPageTemplate, EpdfRenderLayer, EpdfFormLayer], providers: [ provideEmbedPdf( { engine: () => cloudEngine(), initialDocuments: [{ source: invoice }] }, withStage(), withRender(), withInteraction(), withForm(), // The form's own rules are scripts: they run with JavaScript on in the actions plugin. withActions({ javascript: { enabled: true } }), ), ], changeDetection: ChangeDetectionStrategy.OnPush, styleUrl: './validate-values.css', encapsulation: ViewEncapsulation.None, template: `
@if (check(); as check) { @if (check.valid) { Every value passes its field’s rules } @else { } } @else { Not checked yet }

Loading…

`, }) export class App { private readonly form = inject(EpdfForm); protected readonly check = signal(null); /** importValues() writes the values as one change. It runs no field's own rule. */ protected async importOrder() { await this.form.importValues(ORDER); this.check.set(null); } /** Check every value against its field's own rules, and go to the first problem. */ protected async checkValues() { const result = await this.form.validateValues(); this.check.set(result); const first = result.missing[0] ?? result.invalid[0]?.field; if (first) this.form.focus(first.ref); } }