Skip to content

Masks, formatted text, signatures & uploads

Four kinds of answer a plain text box does not give. A mask writes a phone number or an IBAN in its shape once it is typed. Markdown is formatted text with a preview, rendered safely. A signature is drawn - or typed instead - and kept as an image. A file is sent the moment it is chosen, with its progress shown, and the field keeps what came back.

Type the phone number any way you like - 5551234567, 555-123-4567 - and leave the box. The IBAN is stored without its spaces, and shown with them. Open the cover letter's preview and try **bold** or a [link](https://bitplatform.dev); a javascript: link stays text. Sign with the mouse or a finger, or choose Type it instead. The upload here goes nowhere: it reads the file in pieces to move the bar, so pick something of a few megabytes to watch it.
Live form
Application

Formatting help
**bold**
bold
*italic*
italic
`code`
code
[link](https://…)
link
- bulleted list
bulleted list
1. numbered list
numbered list
> quote
quote
# heading
heading

Signature
View source
@page "/rich-fields"

<BormView State="_state" OnValidSubmit="HandleSubmit" SubmitText="Send application" />

@code {
    private readonly Dictionary<string, object?> _data = new();
    private BormState _state = default!;

    protected override void OnInitialized()
    {
        var schema = BormBuilder.Create()
            .Title("Application")
            .Text("Phone", f => f.Required().Mask("(000) 000-0000"))
            .Text("Iban", f => f.Label("IBAN").Mask("AA00 0000 0000 0000 0000 00", storeRaw: true))
            .Text("CoverLetter", f => f.Label("Cover letter").AsMarkdown().Required().MaxLength(2000).CharacterCount())
            .Field("Resume", BormFieldType.File, f => f.Label("Résumé").Accept(".pdf,.doc,.docx,.txt")
                .MaxFileSize(20 * 1024 * 1024).Required().UploadWith(SendAsync))
            .Text("Signature", f => f.AsSignature().Required())
            .Build();

        _state = new BormState(schema, new BormDictionaryDataAccessor(_data));
    }

    // Reads the file as a request body would be read, and returns what storage would.
    private static async Task<string> SendAsync(BormUploadContext upload)
    {
        await using var stream = upload.OpenReadStream();          // counts what is read as sent
        var buffer = new byte[256 * 1024];
        while (await stream.ReadAsync(buffer, upload.CancellationToken) > 0)
            await Task.Delay(40, upload.CancellationToken);       // ... await http.PostAsync(...) ...
        return $"file-{Guid.NewGuid():N}";
    }

    public void Dispose() => _state.Dispose();
}

Masks

In a mask 0 is a digit, A a letter written in capitals, a a letter as typed and * either; a backslash makes the next character literal, and anything else is literal already. When the user leaves the box, what they typed is put into the shape - separators they typed differently are passed over. A value that does not fit is left as typed and reported with the shape to write it in. storeRaw: true keeps only what filled the slots in the model, while the box and a review show it formatted; a mask of digits alone brings up the number pad. The box is deliberately not rewritten under the caret as the user types: that needs script, and assistive technology describes it badly. x-mask and x-maskStoresRaw carry it through JSON Schema.

Markdown and signatures

AsMarkdown() understands paragraphs, headings, lists, quotes, code, rules, **bold**, *italic*, ~~struck~~ and links. Everything the user wrote is HTML-encoded, only those elements are written, a link goes only to http, https or mailto, and links carry rel="nofollow noopener noreferrer". The value is the Markdown text, so length rules count what was typed; BormMarkdown.ToHtml renders it anywhere else and ToPlainText takes the formatting out. It is not a WYSIWYG editor - that needs script, and Borm has none.

AsSignature() is drawn with a mouse, a finger or a pen - the page does not scroll while signing - or typed in a handwriting face. Either way the value is an SVG data: URL an <img> shows as it is, and only a value in that shape is ever shown; BormSignature builds, checks and decodes them. A signature is personal data: mark it Sensitive() to keep it out of drafts.

Uploads

With UploadWith, each file is judged by Accept and MaxFileSize first, then sent, listed with a progress bar and a Cancel button while it goes, Remove once it is there, and Retry - with the reason - if the uploader throws. The field holds what the uploader returned (a list of them for a field taking several), never the file, so it is an answer like any other: it goes into a draft and reaches the server as an id. A submit pressed while a file is on its way waits for it; Cancel, clearing the field or leaving the form cancels upload.CancellationToken. state.UploadsFor(path) lists them for a control of your own. To keep the IBrowserFile on the model instead and read it at submit, see file upload.

An unhandled error has occurred. Reload 🗙