validator

package
v0.0.35 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 11 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("/validate/email", validator.Handler(emailValidator))
r.Get("/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 Route added in v0.0.31

func Route() func(chi.Router)

Route returns a RouteOption that registers all built-in validators.

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
	// Validate is a built-in validation type (e.g. validator.Email).
	// When set, ValidateURL is resolved automatically using BasePath.
	Validate Validation
	// ValidateURL is the endpoint path for server-side validation.
	// The component appends "?id=<ID>" automatically.
	// Overrides Validate if both are set.
	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.

type Validation added in v0.0.31

type Validation string

Validation identifies a built-in server-side validation type.

const (
	// Email validates email format (regex-based).
	Email Validation = "email"
	// EmailMX validates email format and checks MX records.
	EmailMX Validation = "email-mx"
	// IBANValidation validates IBAN format and checksum.
	IBANValidation Validation = "iban"
	// SWIFTValidation validates SWIFT/BIC code format.
	SWIFTValidation Validation = "swift"
)

func (Validation) Path added in v0.0.31

func (v Validation) Path() string

Path returns the handler path for a built-in validation type. Use with webx.Context.APIPath to build the full URL.

Jump to

Keyboard shortcuts

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