Skip to content

Set status code when rendering component #1055

Answered by a-h
JSaterdalen asked this question in Q&A
Discussion options

You must be logged in to vote

re: validation, I cover it using HTMX and templ in the talk I did at Big Sky Dev Con, see https://templ.guide/media/

It uses the approach of creatinf a model that has validation on it:

type Model struct {
	Initial   bool
	UserName  string
	Email     string
	Error     string
}

func (m *Model) ValidateUserName() (msgs []string) {
	if m.Initial { return }
	if m.UserName == "" {
		msgs = append(msgs, "Username is required")
	}
	return msgs
}

func (m *Model) UserNameHasError() bool {
	return len(m.ValidateUserName()) > 0
}

func (m *Model) ValidateEmail() (msgs []string) {
	if m.Initial { return }
	if m.Email == "" || !strings.Contains(m.Email, "@") {
		return append(msgs, "Email is required")…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@JSaterdalen
Comment options

@a-h
Comment options

a-h Jan 28, 2025
Maintainer

@a-h
Comment options

a-h Jan 28, 2025
Maintainer

Answer selected by JSaterdalen
@JSaterdalen
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants