validate missing communication method

This commit is contained in:
Ruidy 2024-03-10 15:40:25 +01:00
parent 3cbddfa47d
commit de8dad8fa8
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 68 additions and 6 deletions

View file

@ -34,8 +34,13 @@ func handleRequestBooking() echo.HandlerFunc {
if to.Sub(from) < time.Duration(0) { if to.Sub(from) < time.Duration(0) {
errs = append(errs, errors.New("invalid_time_range")) errs = append(errs, errors.New("invalid_time_range"))
} }
// a communication mean
// phoneNumber := c.FormValue("phone")
email := c.FormValue("email")
if phoneNumber == "" && email == "" {
errs = append(errs, errors.New("missing_communication_method"))
}
return renderTempl(c, http.StatusOK, view.RequestBookingForm(errs)) return renderTempl(c, http.StatusOK, view.RequestBookingForm(errs))
} }
} }

View file

@ -24,6 +24,15 @@ func invalidTimeRange(errs []error) bool {
return false return false
} }
func missingCommunicationMethod(errs []error) bool {
for _, e := range errs {
if e.Error() == "missing_communication_method" {
return true
}
}
return false
}
templ RequestBookingForm(errors []error) { templ RequestBookingForm(errors []error) {
<form id="booking-request-form" hx-post="/request-booking"> <form id="booking-request-form" hx-post="/request-booking">
<fieldset class="grid"> <fieldset class="grid">
@ -56,11 +65,20 @@ templ RequestBookingForm(errors []error) {
</label> </label>
<label for="phone"> <label for="phone">
Telephone Telephone
<input type="tel" id="phone" name="phone"/> if missingCommunicationMethod(errors) {
<input type="tel" id="phone" name="phone" aria-invalid="true"/>
<small>Veuillez renseigner votre numéro de télephone ou votre adresse email</small>
} else {
<input type="tel" id="phone" name="phone"/>
}
</label> </label>
<label for="email"> <label for="email">
Email Email
<input type="email" id="email" name="email"/> if missingCommunicationMethod(errors) {
<input type="email" id="email" name="email" aria-invalid="true"/>
} else {
<input type="email" id="email" name="email"/>
}
</label> </label>
</fieldset> </fieldset>
<label for="message"> <label for="message">

View file

@ -34,6 +34,15 @@ func invalidTimeRange(errs []error) bool {
return false return false
} }
func missingCommunicationMethod(errs []error) bool {
for _, e := range errs {
if e.Error() == "missing_communication_method" {
return true
}
}
return false
}
func RequestBookingForm(errors []error) templ.Component { func RequestBookingForm(errors []error) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
@ -67,7 +76,7 @@ func RequestBookingForm(errors []error) templ.Component {
var templ_7745c5c3_Var2 string var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(i) templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(i)
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/index.templ`, Line: 33, Col: 29} return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/index.templ`, Line: 42, Col: 29}
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
@ -93,7 +102,37 @@ func RequestBookingForm(errors []error) templ.Component {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }
} }
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</label></fieldset><fieldset class=\"grid\"><label for=\"name\">Nom * <input type=\"text\" id=\"name\" name=\"name\" required></label> <label for=\"phone\">Telephone <input type=\"tel\" id=\"phone\" name=\"phone\"></label> <label for=\"email\">Email <input type=\"email\" id=\"email\" name=\"email\"></label></fieldset><label for=\"message\">Message <textarea name=\"message\" id=\"message\"></textarea></label> <button type=\"submit\">Book</button></form>") _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</label></fieldset><fieldset class=\"grid\"><label for=\"name\">Nom * <input type=\"text\" id=\"name\" name=\"name\" required></label> <label for=\"phone\">Telephone ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if missingCommunicationMethod(errors) {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<input type=\"tel\" id=\"phone\" name=\"phone\" aria-invalid=\"true\"> <small>Veuillez renseigner votre numéro de télephone ou votre adresse email</small>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<input type=\"tel\" id=\"phone\" name=\"phone\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</label> <label for=\"email\">Email ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if missingCommunicationMethod(errors) {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<input type=\"email\" id=\"email\" name=\"email\" aria-invalid=\"true\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<input type=\"email\" id=\"email\" name=\"email\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</label></fieldset><label for=\"message\">Message <textarea name=\"message\" id=\"message\"></textarea></label> <button type=\"submit\">Book</button></form>")
if templ_7745c5c3_Err != nil { if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err return templ_7745c5c3_Err
} }