How to show error in frontend using "html/template"

I am a building a frontend that has an HTML form using “html/template” package, so in form element, I have action="/" attribute where I am doing a post request and saving the form data. My saving in DB is working perfectly.
I want to know how to show errors?
I am using validator/v10 package to do validation, I am able to get errors in JSON response but not understanding how to render that on the front end

I understand that I have to make a function ErrorsStub() and pass it in template.FuncMap{
“errors”: ErrorsStub,
and then check errors on frontend
How to achieve that?

adding a code example
<form action="/" method = "post">
<input class="{{if errors}}border-red-600{{end}}" {{with .Name}}id="{{.}}"{{end}} type="{{.Type}}" name="{{.Name}}" placeholder="{{.Placeholder}}" {{with .Value}}value="{{.}}"{{end}}> {{range errors}} <p class="text-red-500 pt-2 text-xs italic">{{.}}</p> {{end}}
<button class="bg-teal-400 hover:bg-teal-500 text-white font-bold py-2 px-4 rounded" type="submit"> Submit </button>

this is my form.html which would be parsed by

pageTpl := template.Must(template.New("").Funcs(template.FuncMap{
	"errors": ErrorsStub,

and then pageTpl would be executed in get request at “/” endpoint by pageTpl.ExecuteTemplate with data passed in the template

I have everything working (my endpoints are parsed and data is being saved in DB)

I just have to implement

func ErrorsStub() []string {
return []string{}

to get errors from the back end that I can display in my templates frontend

I Interpret this that you have to have to involve javascript in some way. Maybe via another endpoint.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.