Add BE validator
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"myapp/internal/cards/encryption"
|
||||
"myapp/internal/models"
|
||||
"myapp/internal/urlsigner"
|
||||
"myapp/internal/validator"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -124,6 +125,14 @@ func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter,
|
||||
return
|
||||
}
|
||||
|
||||
// validate data
|
||||
v := validator.New()
|
||||
v.Check(len(data.FirstName) > 1, "first_name", "must be at least 2 characters")
|
||||
if !v.Valid() {
|
||||
app.failedValidation(w, r, v.Errors)
|
||||
return
|
||||
}
|
||||
|
||||
app.infoLog.Println(data.Email, data.LastFour, data.PaymentMethod, data.Plan)
|
||||
|
||||
card := cards.Card{
|
||||
|
@ -95,3 +95,20 @@ func (app *application) passwordMatches(hash, password string) (bool, error) {
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (app *application) failedValidation(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
errors map[string]string,
|
||||
) {
|
||||
var payload struct {
|
||||
OK bool `json:"ok"`
|
||||
Message string `json:"message"`
|
||||
Errors map[string]string `json:"errors"`
|
||||
}
|
||||
|
||||
payload.OK = false
|
||||
payload.Message = "failed validation"
|
||||
payload.Errors = errors
|
||||
app.writeJSON(w, http.StatusUnprocessableEntity, payload)
|
||||
}
|
||||
|
@ -14,18 +14,22 @@ Bronze Plan
|
||||
class="d-blick needs-validation charge-form"
|
||||
autocomplete="off"
|
||||
novalidate="">
|
||||
<input type="hidden" id="product_id" name="product_id" value="{{$widget.ID}}">
|
||||
<input type="hidden"
|
||||
id="product_id"
|
||||
name="product_id"
|
||||
value="{{$widget.ID}}">
|
||||
<input type="hidden" id="amount" name="amount" value="{{$widget.Price}}">
|
||||
<p class="mt-2 mb-3 text-center">{{$widget.Description}}</p>
|
||||
<hr>
|
||||
<div class="mb-3">
|
||||
<label for="first-name" class="form-label">First Name</label>
|
||||
<label for="first_name" class="form-label">First Name</label>
|
||||
<input type="text"
|
||||
id="first-name"
|
||||
id="first_name"
|
||||
name="first_name"
|
||||
autocomplete="first-name-new"
|
||||
autocomplete="first_name-new"
|
||||
required=""
|
||||
class="form-control">
|
||||
<div id="first_name-help" class="d-none"></div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="last-name" class="form-label">Last Name</label>
|
||||
|
Reference in New Issue
Block a user