Add BE validator
This commit is contained in:
parent
082c75283d
commit
3d5212fb53
@ -9,6 +9,7 @@ import (
|
|||||||
"myapp/internal/cards/encryption"
|
"myapp/internal/cards/encryption"
|
||||||
"myapp/internal/models"
|
"myapp/internal/models"
|
||||||
"myapp/internal/urlsigner"
|
"myapp/internal/urlsigner"
|
||||||
|
"myapp/internal/validator"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -124,6 +125,14 @@ func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter,
|
|||||||
return
|
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)
|
app.infoLog.Println(data.Email, data.LastFour, data.PaymentMethod, data.Plan)
|
||||||
|
|
||||||
card := cards.Card{
|
card := cards.Card{
|
||||||
|
@ -95,3 +95,20 @@ func (app *application) passwordMatches(hash, password string) (bool, error) {
|
|||||||
|
|
||||||
return true, nil
|
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"
|
class="d-blick needs-validation charge-form"
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
novalidate="">
|
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}}">
|
<input type="hidden" id="amount" name="amount" value="{{$widget.Price}}">
|
||||||
<p class="mt-2 mb-3 text-center">{{$widget.Description}}</p>
|
<p class="mt-2 mb-3 text-center">{{$widget.Description}}</p>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="mb-3">
|
<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"
|
<input type="text"
|
||||||
id="first-name"
|
id="first_name"
|
||||||
name="first_name"
|
name="first_name"
|
||||||
autocomplete="first-name-new"
|
autocomplete="first_name-new"
|
||||||
required=""
|
required=""
|
||||||
class="form-control">
|
class="form-control">
|
||||||
|
<div id="first_name-help" class="d-none"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="last-name" class="form-label">Last Name</label>
|
<label for="last-name" class="form-label">Last Name</label>
|
||||||
|
25
internal/validator/validator.go
Normal file
25
internal/validator/validator.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package validator
|
||||||
|
|
||||||
|
type Validator struct {
|
||||||
|
Errors map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Validator {
|
||||||
|
return &Validator{Errors: make(map[string]string)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Validator) Valid() bool {
|
||||||
|
return len(v.Errors) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Validator) AddError(key, message string) {
|
||||||
|
if _, exists := v.Errors[key]; !exists {
|
||||||
|
v.Errors[key] = message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Validator) Check(ok bool, key, message string) {
|
||||||
|
if !ok {
|
||||||
|
v.AddError(key, message)
|
||||||
|
}
|
||||||
|
}
|
@ -47,7 +47,7 @@ function stripePaymentMethodHandler(result, plan_id, api) {
|
|||||||
card_brand: result.paymentMethod.card.brand,
|
card_brand: result.paymentMethod.card.brand,
|
||||||
expiry_month: result.paymentMethod.card.exp_month,
|
expiry_month: result.paymentMethod.card.exp_month,
|
||||||
expiry_year: result.paymentMethod.card.exp_year,
|
expiry_year: result.paymentMethod.card.exp_year,
|
||||||
first_name: document.getElementById("first-name").value,
|
first_name: document.getElementById("first_name").value,
|
||||||
last_name: document.getElementById("last-name").value,
|
last_name: document.getElementById("last-name").value,
|
||||||
amount: document.getElementById("amount").value,
|
amount: document.getElementById("amount").value,
|
||||||
};
|
};
|
||||||
@ -65,9 +65,22 @@ function stripePaymentMethodHandler(result, plan_id, api) {
|
|||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(function (data) {
|
.then(function (data) {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
if (data.ok === false) {
|
||||||
|
document.getElementById("charge_form").classList.remove("was-validated")
|
||||||
|
Object.entries(data.errors).forEach((i) => {
|
||||||
|
const [key, value] = i
|
||||||
|
document.getElementById(key).classList.add("is-invalid");
|
||||||
|
document.getElementById(key + "-help").classList.remove("valid-feedback");
|
||||||
|
document.getElementById(key + "-help").classList.remove("d-none");
|
||||||
|
document.getElementById(key + "-help").classList.add("invalid-feedback");
|
||||||
|
document.getElementById(key + "-help").innerText = value;
|
||||||
|
});
|
||||||
|
showPayButton();
|
||||||
|
return
|
||||||
|
}
|
||||||
processing.classList.add("d-none");
|
processing.classList.add("d-none");
|
||||||
showSuccess("card-messages", "Transaction successful!");
|
showSuccess("card-messages", "Transaction successful!");
|
||||||
sessionStorage.first_name = document.getElementById("first-name").value;
|
sessionStorage.first_name = document.getElementById("first_name").value;
|
||||||
sessionStorage.last_name = document.getElementById("last-name").value;
|
sessionStorage.last_name = document.getElementById("last-name").value;
|
||||||
sessionStorage.amount = document.getElementById("amount").value;
|
sessionStorage.amount = document.getElementById("amount").value;
|
||||||
sessionStorage.last_four = result.paymentMethod.card.last4;
|
sessionStorage.last_four = result.paymentMethod.card.last4;
|
||||||
|
Loading…
Reference in New Issue
Block a user