validator

package
v0.0.29 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

templ: version: v0.3.977

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(fn ValidateFunc) http.HandlerFunc

Handler returns an http.HandlerFunc for a single ValidateFunc. The component ID is passed as a query parameter "id".

Mount each validator at its own path:

r.Get("/api/validate/email", validator.Handler(emailValidator))
r.Get("/api/validate/phone", validator.Handler(phoneValidator))

The component references this path via the ValidateURL prop.

func Input

func Input(props InputProps) templ.Component

Input renders a DaisyUI input with server-side validation via Datastar. On input change (debounced), it calls the backend validator and shows/hides a validation hint based on the result.

func SuccessHint

func SuccessHint(id string) templ.Component

SuccessHint renders a hint that is visible only when validation succeeds and the input has a non-empty value.

Types

type InputProps

type InputProps struct {
	// ID uniquely identifies this validator instance. Required.
	ID string
	// Class adds additional CSS classes to the input element.
	Class string
	// Attributes adds arbitrary HTML attributes to the input element.
	Attributes templ.Attributes
	// Type is the HTML input type. Defaults to TypeText.
	Type InputType
	// Name is the form field name attribute.
	Name string
	// Placeholder is the input placeholder text.
	Placeholder string
	// Value is the initial input value.
	Value string
	// ValidateURL is the endpoint path for server-side validation.
	// The component appends "?id=<ID>" automatically.
	// Example: "/api/validate/email"
	ValidateURL string
	// DebounceMs is the debounce delay in milliseconds. Defaults to 500.
	DebounceMs int
	// HintText is the error hint shown when validation fails.
	// If empty, the backend error message is used.
	HintText string
}

InputProps configures a validated input field.

type InputType

type InputType string

InputType is the HTML input type attribute.

const (
	TypeText     InputType = "text"
	TypeEmail    InputType = "email"
	TypePassword InputType = "password"
	TypeTel      InputType = "tel"
	TypeURL      InputType = "url"
	TypeNumber   InputType = "number"
	TypeSearch   InputType = "search"
)

type Result

type Result struct {
	Valid bool
	Error string
}

Result holds the outcome of a validation check.

type ValidateFunc

type ValidateFunc func(value string) Result

ValidateFunc validates a string value and returns a Result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL